1

When I add a $broadcast in $rootScope and catch it to other controller, it is not reflecting any value to view. Example:

// on first ctrl.
controller('firstCtrl', ['$scope','$location', '$rootScope', function($scope, $location, $rootScope) {
    $scope.myFunc = function() { 
        $rootScope.$broadcast('someEvent');
        $location.path('/users');
    }
}])


// On Users Page.
.controller('usersCtrl', ['$scope','$rootScope', function($scope, $rootScope) { 
    $scope.dataFilter = {};
    $rootScope.$on('someEvent', function(event, b) {
        $scope.callOnBroadcast();
    });
  
    // call this function on broadcast.  
    $scope.callOnBroadcast = function() {
      $scope.dataFilter = {
        types: [
          {key: 'tags',     val: 'Collections'},
          {key: 'prices',   val: 'Price'},
          {key: 'ratings',  val: 'Rating'},
          {key: 'designers',val: 'Designer'}
        ],
        data: {tags: [], prices: [], ratings: [], designers: []}

      };

      $scope.$apply();


    };
}])
<h4 data-ng-repeat="ftr in dataFilter.types">  
  {{ftr.val}}
</h4>

When I Use MyFunc function on firstCtrl, it will redirect me to user's page, also broadcast function run. On users page, I Use $scope.callOnBroadcast() when broadcast is on, but it is reflecting any change on view page even I use $scope.$apply(). Where am I wrong ?

3
  • 1
    do $rootScope$broadcast('someEvent'); if controller hierarchy is not there..Please add html to see how your controller hierarchy Commented Apr 9, 2015 at 5:17
  • here is no controllers hierarchy, that is why I added $broadcast in $rootScope. Commented Apr 9, 2015 at 5:40
  • oh sorry its a copy paste mistake. I already use $broadcast in $roootScope. Commented Apr 9, 2015 at 5:50

3 Answers 3

2

Your both the controller don't have any hierarchy that's why you need to $broadcast the event in $rootScope rather than scope,

Your problem is you are broadcasting event before registering listener event. After that you are doing $location.path('/users'), which load user template with usersCtrl.

I think you should do redirection 1st and then broadcast the event in $rootScope with certain timeout inorder to make available the usersCtrl

Code

// on first ctrl.
controller('firstCtrl', ['$scope','$location', '$rootScope',  '$timeout',function($scope, $location, $rootScope, $timeout) {
    $scope.myFunc = function() { 
        $location.path('/users');
        $timeout(function(){
          //boardcast will available to every listener
          $rootScope.$broadcast('someEvent'); 
        },1000);
    }
}]);

Make sure the other listener code should be register before broadcasting the event.(Below listener should be registered before broadcasting).

$scope.$on('someEvent', function(event, b) {
    $scope.callOnBroadcast();
});

For making it more better solution, you could use resolve of $routeProvider

Sign up to request clarification or add additional context in comments.

5 Comments

could you please confirm that controller has been loaded or not before broadcasting event
yes controller is loading and if I add alert in '$scope.callOnBroadcast()', It is reflecting.
@Rajeev no...your code clearly says that you are broadcasting event then doing $location.path('/users'); which is wrong..do $location.path('/users'); first and then do braodcast
I updated my code like you mentioned above but it is not working.
So basically the issue was that your controller wasn't instantiated :) Just giving it a second to load before you broadcast is the trick.
0

Have a look at this "How Scope.$broadcast() Interacts With Isolate Scopes In AngularJS" http://www.bennadel.com/blog/2725-how-scope-broadcast-interacts-with-isolate-scopes-in-angularjs.htm

It will be helpful to you.

Comments

0

You can inject $rootScope into your controllers and broadcast on that level. You can see the $broadcast taking effect here: http://plnkr.co/edit/ySrEU4accdgeY7iJhdZi?p=preview

app.controller('firstCtrl', ['$scope', '$rootScope','$location', function($scope, $rootScope, $location) {
$scope.myFunc = function() { 
    $rootScope.$broadcast('someEvent');
    $location.path('/users');
};

}])


// On Users Page.
.controller('usersCtrl', ['$scope', '$rootScope', function($scope, $rootScope) { 
$scope.dataFilter;
$rootScope.$on('someEvent', function(event, b) {
    $scope.callOnBroadcast();
    //$scope.dataFilter = 'test';
});

// call this function on broadcast.  
$scope.callOnBroadcast = function() {
  $scope.dataFilter = {
    types: [
      {key: 'tags',     val: 'Collections'},
      {key: 'prices',   val: 'Price'},
      {key: 'ratings',  val: 'Rating'},
      {key: 'designers',val: 'Designer'}
    ],
    data: {tags: [], prices: [], ratings: [], designers: []}

  };

  //$scope.$apply();  <- I don't think is necessary


};

}])

3 Comments

The code is working if I use $rootScope.dataFilter instead of $scope.dataFilter. I don't know why this happen. As view has "usersCtrl", so their is no need to add $rootScope.
Just to confirm - the usersCtrl is already instantiated when you call the function, right?
right, ctrl instantiated first and then function call

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.