如果我有以下内容:
myapp.directive('directivename', ...
return {
...
restrict: 'E',
controller: MyController,
...
}
function MyController($scope, $somethingelse) {
// Contents of controller here
}
);
如何修改它,使 MyController
在缩小时不会被销毁?
我收到以下错误:
Error: [$injector:unpr] Unknown provider: eProvider <- e
最佳答案
可以使用显式依赖注解来解决。你所拥有的隐式注释会在缩小时引起问题。您也可以使用 $inject
或内联数组注释来注释指令中的依赖项。
MyController.$inject = ['$scope', '$somethingelse'];
function MyController($scope, $somethingelse) {
// Contents of controller here
}
或者在指令中:
return {
...
restrict: 'E',
controller: ['$scope', '$somethingelse', MyController],
...
}
或者使用 .controller
语法注册你的 Controller
app.controller('MyController', ['$scope', '$somethingelse', MyController]);
并在指令中设置 Controller 名称而不是构造函数。
return {
...
restrict: 'E',
controller: 'MyController',
...
}
你也可以看看 ng-annotate 您不需要使用显式注释。
关于javascript - 使用指令 Controller 进行 Angular 缩小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27727017/