How to target a specific Ionic view with $rootScope.$broadcast

Using Ionic, calling $rootScope.$broadcast("$ionicView.enter") will trigger all child views having an event handler for the $ionicView.enter event.

How can I narrow this down to target the handler for a specific view?

You haven't provided any code samples, but it sounds like what you are describing doesn't require triggering an event with specific data, but listening to the event w/ specific data.

Perhaps this snippet can help you:

$rootScope.$on( "$ionicView.enter", function( scopes, states ) {
        //here's an example of storing network error state in rootscope 
        //and some random important state that requires your data to be refreshed 

        if( $rootScope.networkErrorOccurred && states.stateName == "myImportantState" ) {
           //refresh data due to network error code goes here 
        }
    });

When calling $rootScope.$broadcast("$ionicView.enter") the "states" object is none existent and hence you cannot rely on the stateName.

But, you can add it yourself, like so:

$rootScope.$broadcast('$ionicView.enter', {stateName: 'myImportantState'});

This will make Travis example working.


The other way I can think of is to save a reference to the view's scope and emit an event directly on it.

I must say I wouldn't recommend it, but maybe that's what you're looking for.

Inside the view's controller

app.controller('someViewCtrl', function($scope) {
  //Save a reference to the view's scope, using window for brevity
  window.someViewScope = $scope;
}

Somewhere else in the app

window.someViewScope.$emit('$ionicView.enter')