我试图通过在指令使用的控制器中使用原型继承来优化我的代码。由于可以多次调用指令,我认为这样做可能是个好主意。但是我的控制器原型取决于注入的服务,有角度的抱怨它无法访问。
(function (angular) {
'use strict';
angular.module('myModule', [])
.factory('MyService', MyService)
.controller('myController', MyController);
// My service
function MyService() {
return {
getMyStuff: getMyStuff
}
function getMyStuff() {
return 'stuff';
}
}
// My controller
MyController.$inject = ['MyService'];
function MyController(MyService) {
var ctrl = this;
// Provide some value to the template
}
MyController.prototype = {
getStuff: function () {
return MyService.getMyStuff(); // This does not work. MyService is not available from the prototype. Why??
}
}
})(angular);
如何让MyService可用于原型?
我甚至尝试使用$ injector服务来获取它,但是
getStuff: function () {
return angular.injector(['myModule']).get('MyService').getMyStuff();
}
抱怨更多......