如何在AngularJs中设置来自其提供商的服务的依赖关系
How to set the dependencies of a service from its provider in AngularJs?
我正在尝试执行以下操作:
var myApp = angular.module('myApp', []);
myApp.provider('routeResolver', ['$q','$rootScope',function($q, $rootScope)
{
console.log( $q, $rootScope );
this.$get = function()
{
return this;
}
}]
);
然而,这会产生错误:Unknown Provider $q
所以我把代码改为:
myApp.provider('routeResolver', ['$qProvider','$rootScopeProvider',function($q, $rootScope)
{
console.log( $q.defer() );
console.log( $rootScope );
this.$get = function()
{
return this;
}
}]
);
然而,这给出了CCD_ 2的误差。甚至做:
console.log( $q.$get().defer() );
不起作用。有什么想法吗?
代替:
this.$get = function()
{
return this;
}
试试这个:
return {
// object being returned has to implement $get method
$get: function() {
// you should be able to use $q and $rootScope here
return this;
}
};
除了$get
,还应该有其他方法可以访问所有注入的服务:
return {
// object being returned has to implement $get method
$get: function() {
// you should be able to use $q and $rootScope here
return this;
},
// you can also have more methods here
someOtherMethod: function() {
// you should be able to use $q and $rootScope here as well
// for example:
$rootScope.$apply();
}
};
这是假设您将$q
和$rootScope
注入构造函数(无论您是否使用数组表示法)。
这就是我从这里和这里推断出的
相关文章:
- AWS Cognito:通过 google 获取“请提供有效的公共提供商”
- 角度模态服务未知提供商
- 选择下拉列表需要在选中时显示提供商的详细信息-无法通过我的脚本显示url
- 提供“角服务”;TypeError:无法读取属性'helloConsole'“未定义”;
- 从服务提供商 IP 地址获取设备 IP 地址
- JsFiddle:未知提供商:$intervalProvider <- $interval
- 为根范围承诺提供角度服务
- 开发两个单独的节点应用程序来提供 Web 服务和使用 Web 服务以在浏览器上呈现它是一个好主意吗?
- 我可以在 angle js asp.net mvc 中调用控制器中的路由提供程序服务吗?
- ES2015 单例或服务提供商或模块,用于敲除.js组件
- Angular Js:工厂、服务和提供商.何时使用哪个
- 为什么我必须添加尾部'提供商'到服务名称,当我使用es6时
- 如何在AngularJs中设置来自其提供商的服务的依赖关系
- 在AngularJS中,当自定义服务注入到不同的模块时,未知的提供商
- forEach内部提供商服务AngularJS
- 使用Jasmine在AngularJS中测试服务时,未知提供商
- Angular:在模块's的config/run中混合了提供商和自定义服务
- 未知的提供商:userProvider为现有的约曼AngularFire用户服务
- 如何使用angular 1.3将服务注入提供商
- Angular.js+Karma+Jasmine:未知服务提供商