Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
135
rated 0 times [  138] [ 3]  / answers: 1 / hits: 16373  / 11 Years ago, sun, july 21, 2013, 12:00:00

I have the following set up:



stApp.controller('AdminTableController', ['$rootScope', '$scope', 'gridService', 
function ($rootScope, $scope, gridService) {
$scope.$watch('tableData.$pristine', function (newValue) {
$rootScope.broadcast(tableDataUpdated, {
state: page.$pristine
});
});
}])

stApp.controller('AdminGridController', ['$rootScope', '$scope', 'gridService',
function ($rootScope, $scope, gridService) {
$rootScope.on(tableDataUpdated, function (args) {
//args.state would have the state.
alert(args.state);
});
}])


When I run this code I am getting a message:



Object #<Object> has no method 'on'


Note that I tried this with both $rootScope.on and $scope.on


More From » angularjs

 Answers
14

You must have meant $broadcast and $on (rather than broadcast and on), that is:



$rootScope.$broadcast(tableDataUpdated, { state: page.$pristine });
// ...
$rootScope.$on(tableDataUpdated, function (args) {
// ...


It's worth noting that $broadcast is used to delegate events to child or sibling scopes, whereas $emit will bubble events upwards to the scope's parents, hence;



When choosing $broadcast (and not $emit), one should either inject the root scope for tying the $on (as you nicely did) or call $on on the receiver's isolated scope, be it a child scope of the dispatcher.



See this post for elaboration on $emit and $broadcast.


[#76841] Friday, July 19, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
makaylahk

Total Points: 166
Total Questions: 94
Total Answers: 117

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
;