[英]Angular, injecting own service into provider
So I am working with a provider and injecting some of angular's $log and $location service in the $get to work with them, and this works fine. 因此,我正在与提供者合作,并在$ get中注入一些angular的$ log和$ location服务,以便与他们合作,这很好。 However when I want to inject my own service into the provider it does not seem to work.
但是,当我想将自己的服务注入提供程序时,它似乎不起作用。
Here's what I mean - I can inject the angular $log and $location like so (in the provider) 这就是我的意思-我可以像这样在提供程序中注入有角的$ log和$ location
this.$get = $get;
$get.$inject = ['$log', '$location'];
function $get( $log, $location) {
return {
setModule: function(name, cb) {
},
getModules: function() {
}
}
}
But, I have a service below like 但是,我下面有一项服务
.service("myService",function(){
this.sayHello = function() {
return "Hello, World!"
});
And try to inject it like 并尝试像
$get.$inject = ['myService', '$log', '$location'];
function $get(myService, $log, $location) {
When I call myService in the provider, it comes back undefined. 当我在提供程序中调用myService时,它未定义返回。
Is there any reason I can call the angular($) stuff and not my own to inject into the provider? 有什么理由可以调用angular($)而不是我自己的东西注入提供程序吗?
Thanks! 谢谢!
Edit : So after I inject it I try and use it inside the $get like 编辑:因此,在我注入后,我尝试在$ get之类的地方使用它
$get.$inject = ['myService', '$log', '$location'];
function $get(myService, $log, $location) {
return {
test: function(){
console.log(myService);
}
}
returns undefined 返回未定义
1) pass $injector as a dependency in the controller ['$injector'] 1)将$ injector作为依赖项传递给控制器['$ injector']
2) call $injector.get to instantiate the service by name 2)调用$ injector.get通过名称实例化服务
$scope.service = $injector.get(servicename); $ scope.service = $ injector.get(服务名);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.