Javascript 如何在控制器中动态注入依赖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12758157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to $inject dynamically dependence in a controller
提问by Yacine Rezgui
I'm still a debutant on Angularjs. I want to inject dynamically a dependency of a service (that I created) in my controller.
我仍然是 Angularjs 的新手。我想在我的控制器中动态注入一个服务(我创建的)的依赖项。
But when I code a service with dependencies, I got this error :
但是,当我编写具有依赖项的服务时,出现此错误:
Error: Unknown provider: $windowProvider <- $window <- base64
错误:未知提供者:$windowProvider <- $window <- base64
This is the code of the controller.
这是控制器的代码。
var base64 = angular.injector(['servicesModule']).get('base64');
console.log("base64", base64.encode("my text will be encoded"));
This code works:
此代码有效:
var servicesModule = angular.module('servicesModule', []);
servicesModule.factory('base64', function() {
return {
name: 'base64',
readonly: false,
encode: function(input) {
return window.btoa(input);
},
decode: function(input) {
return window.atob(input);
}
};
});
This code doesn't work :
此代码不起作用:
var extModule = angular.module('ext', []);
extModule.factory('base64', ['$window', function($window) {
return {
name: 'base64',
readonly: false,
encode: function(input) {
return $window.btoa(input);
},
decode: function(input) {
return $window.atob(input);
}
};
}]);
Another problem is when the service is in the same module as the controller. If the module has dependencies, I doesn't work (I have $routeProvider dependence in my module config) :
另一个问题是当服务与控制器在同一个模块中时。如果模块有依赖项,我将无法工作(我的模块配置中有 $routeProvider 依赖项):
Error: Unknown provider: $routeProvider from mainModule
错误:未知提供者:来自 mainModule 的 $routeProvider
var mainModule = angular.module('main', [],
function($routeProvider, $locationProvider) {
//Some routing code
}
);
JS Fiddles
JS小提琴
Same module with dependencies(controller + service) : http://jsfiddle.net/yrezgui/YedT2/
具有依赖项(控制器 + 服务)的相同模块:http: //jsfiddle.net/yrezgui/YedT2/
Different module with dependencies : http://jsfiddle.net/yrezgui/YedT2/4/
具有依赖项的不同模块:http: //jsfiddle.net/yrezgui/YedT2/4/
Different module without dependencies : http://jsfiddle.net/yrezgui/YedT2/5/
没有依赖项的不同模块:http: //jsfiddle.net/yrezgui/YedT2/5/
回答by Mark Rajcok
Don't call angular.injector() -- this creates a new injector. Instead, inject the already-created $injector
into your controller and use it:
不要调用 angular.injector() —— 这会创建一个新的注入器。相反,将已经创建的$injector
注入您的控制器并使用它:
So instead of:
所以而不是:
var algoController = function($scope) {
$scope.base64 = angular.injector(['main']).get('base64');
};
Do this:
做这个:
var algoController = function($scope, $injector) {
$scope.base64 = $injector.get('base64');
};
But most of the time you should inject your service directly, rather than dynamically, like so:
但是大多数时候你应该直接注入你的服务,而不是动态注入,像这样:
var algoController = function($scope, base64) {
$scope.base64 = base64;
};