I want to be able to specify the controller of my directive with an inline function, but I also want to use ng-strict-di. What syntax is required to do this?
我希望能够使用内联函数指定我的指令的控制器,但我也想使用ng-strict-di。这样做需要什么语法?
(function(){
angular.module("myAngularModule")
.directive("myDirective", function(){
return {
restrict: 'E',
templateUrl: "templates/my-template.html",
link: function ($scope, element, attrs) {
// ...
},
// This causes an ng-strict-di exception because I'm using implicit annotation for the dependencies - what is the correct syntax?
controller: function($scope, myService) {
// ...
}
};
})
// This syntax is fine
.controller("myWorkingController",["$scope","myService", function($scope, myService){
// ...
}]);
});
2 个解决方案
#1
3
Just because the controller is anonymous doesn't meant the syntax changes. Pass an array like you would any other controller assignment. Angular will understand.
仅仅因为控制器是匿名的并不意味着语法改变。像任何其他控制器分配一样传递数组。 Angular会理解。
controller: ["$scope","myService", function($scope, myService){
// ...
}]
#2
2
Inject the service in to your directive
as, its something like injecting into the controller,
将服务注入到您的指令中,就像注入控制器一样,
.directive("myDirective", function(myService){
and remove it from the controller.
并将其从控制器中删除。
.directive("myDirective", function(myService){
return {
restrict: 'E',
templateUrl: "templates/my-template.html",
link: function ($scope, element, attrs) {
// ...
},
controller: function($scope) {
// ...
}
};
})
then myService
can be access in the controller
of the directive
.
然后myService可以在指令的控制器中访问。
#1
3
Just because the controller is anonymous doesn't meant the syntax changes. Pass an array like you would any other controller assignment. Angular will understand.
仅仅因为控制器是匿名的并不意味着语法改变。像任何其他控制器分配一样传递数组。 Angular会理解。
controller: ["$scope","myService", function($scope, myService){
// ...
}]
#2
2
Inject the service in to your directive
as, its something like injecting into the controller,
将服务注入到您的指令中,就像注入控制器一样,
.directive("myDirective", function(myService){
and remove it from the controller.
并将其从控制器中删除。
.directive("myDirective", function(myService){
return {
restrict: 'E',
templateUrl: "templates/my-template.html",
link: function ($scope, element, attrs) {
// ...
},
controller: function($scope) {
// ...
}
};
})
then myService
can be access in the controller
of the directive
.
然后myService可以在指令的控制器中访问。