1

I want to use https://github.com/alexcrack/angular-ui-notification for notifications. I need them in all my controllers. Is it possible to inject 'Notification'(or '$log' or whatever) in all my controllers?

Roman Koliada
  • 4,286
  • 2
  • 30
  • 59

1 Answers1

5

I think you could by letting your controllers inherit from a common basecontroller. Something like this might work:

angular.module('extending', [])

.controller('baseController', function(someService) {
   this.someService = someService;
})

.controller('extendedController', function($scope, $controller) {
  angular.extend(this, $controller('baseController', { $scope: $scope }));

   this.alert = this.someService.alert;
})

.service('someService', function() {
  this.alert = function() {
    window.alert('alert some service');
  };
});

HTML

  <body>
    <div ng-controller="extendedController as ex">
        <button ng-click="ex.alert()">Alert</button>
    </div>
  </body>

Example on plunker. Related post on SO. AngularjS extend doc.

Community
  • 1
  • 1
skubski
  • 1,586
  • 2
  • 19
  • 25