简体   繁体   中英

How to set a listener to every new scope created though ng-view directive or ng-controller directive?

I would like have a on listener something like below for all controllers which are instantiated. And, I don't want to include this expression in all of my controllers. I just wanted to set this listener on all new scopes created in angualr js application.

$scope.$on('somebroadcastedevent', function someEvent() {

});

How can I accomplish?

Override ngController directive.

module.directive('ngController', function () {
   return {
      link: function (scope) {
          // register the listener here. 
          scope.$on('somebroadcastedevent', function someEvent() {

          });
      }
   }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM