我想將One Controller注入服務。AngularJS:如何將Controller注入服務?
我用AngularJs和Laravel和glup-NG-註釋。
/* 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')
);
}
};
});
})();
我有這樣的錯誤
錯誤:[$注射器:unpr]未知提供商:DialogControllerProvider < - DialogController < - DialogService
工廠/服務可以擁有自己的控制器!您不能將控制器添加/注入到服務/工廠! –
你試圖把車放在馬前。 – JLRishe
看看這個JS代碼,第82和108行,http://codepen.io/kyleledbetter/pen/gbQOaV –