【发布时间】:2015-08-28 08:32:12
【问题描述】:
我想使用https://github.com/alexcrack/angular-ui-notification 进行通知。我所有的控制器都需要它们。是否可以在我的所有控制器中注入“通知”(或“$log”或其他)?
【问题讨论】:
-
您只需在控制器中加载
Notification服务作为依赖项。
标签: angularjs dependency-injection
我想使用https://github.com/alexcrack/angular-ui-notification 进行通知。我所有的控制器都需要它们。是否可以在我的所有控制器中注入“通知”(或“$log”或其他)?
【问题讨论】:
Notification 服务作为依赖项。
标签: angularjs dependency-injection
我认为你可以通过让你的控制器从一个通用的基本控制器继承。这样的事情可能会起作用:
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>
【讨论】:
extend 功能,+1