javascript - How can I send a message from one controller to another in AngularJS? -
i have 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 have state. alert(args.state); }); }]) when run code getting message:
object #<object> has no method 'on' note tried both $rootscope.on , $scope.on
you must have meant $broadcast , $on (rather broadcast , on), is:
$rootscope.$broadcast("tabledataupdated", { state: page.$pristine }); // ... $rootscope.$on("tabledataupdated", function (args) { // ... it's worth noting $broadcast used delegate events child or sibling scopes, whereas $emit bubble events upwards scope's parents, hence;
when choosing $broadcast (and not $emit), 1 should either inject root scope tying $on (as nicely did) or call $on on receiver's isolated scope, child scope of dispatcher.
see this post elaboration on $emit , $broadcast.
Comments
Post a Comment