我想将一个 Controller 注入(inject)服务。
我使用 AngularJs 和 Laravel 以及 glup-ng-annotate。
/* DialogController*/
(function(){
"use strict";
angular.module("music.controllers").controller('DialogController', function( $scope, $mdDialog ){
$scope.hide = function() {
$mdDialog.hide();
};
$scope.cancel = function() {
$mdDialog.cancel();
};
$scope.answer = function(answer) {
$mdDialog.hide(answer);
};
});
})();
这是服务
/* Service */
(function(){
"use strict";
angular.module("music.services").factory('DialogService', function( $mdDialog, DialogController){
return {
fromTemplate: function(template, $scope ) {
var options = {
controller: DialogController,
templateUrl: template
};
if ( $scope ){
options.scope = $scope.$new();
}
return $mdDialog.show(options);
},
alert: function(title, content){
$mdDialog.show(
$mdDialog.alert()
.title(title)
.content(content)
.ok('Ok')
);
}
};
});
})();
我有这个错误
错误:[$injector:unpr] 未知提供者:DialogControllerProvider <- DialogController <- DialogService
最佳答案
可以将服务注入(inject) Controller ,但反之则不行。由于 AngularJS 中的依赖注入(inject)支持在 Controller 中注入(inject)服务。
关于 Angular : How to inject Controller into Service?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36975010/