angularjs - Angular: broadcast event from service -


i create custom event in angular. goal event broadcast service , must catched in controller.

i have in service :

function dosomething() {         // something...         $rootscope.$broadcast("bced");         $rootscope.$emit("emitted");         console.log('event'); } 

and controller :

$rootscope.$on("bced",function(){     console.log("catched"); }); $rootscope.$on("emitted",function(){     console.log("catched"); });  $scope.$on("bced",function(){     console.log("catched"); }); $scope.$on("emitted",function(){     console.log("catched"); }); 

but can't catch events in controller. console.log('event') shown can't catch events. explain me how please? $rootscope correctly declared in service , in controller.

checkout working demo:

var app = angular.module('core', []);      app.service('someservice', function($timeout, $rootscope){          this.generateevent = function(){              $timeout(function(){                  $rootscope.$broadcast('event:show', {some: 'data'});              }, 3000);              }                })            app.controller('maincontroller', function($scope, someservice){          someservice.generateevent();          $scope.$on('event:show', function(event, data){              $scope.test = data.some;          })      })
<div ng-app="core" ng-controller="maincontroller">      {{test}}  </div>    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -