在应用配置angular.js中的自定义提供程序中使用$http
use $http inside custom provider in app config, angular.js
主要问题-这可能吗?我尝试了,但没有成功。。
main app.js
...
var app = angular.module('myApp', ['services']);
app.config(['customProvider', function (customProvider) {
}]);
...
提供商本身
var services = angular.module('services', []);
services.provider('custom', function ($http) {
});
我有这样的错误:
Uncaught Error: Unknown provider: $http from services
有什么想法吗?
谢谢!
底线是:
- 您不能将服务注入提供程序配置部分
- 您CAN将服务注入初始化提供商服务的部分
详细信息:
Angular框架有两个阶段的初始化过程:
阶段1:配置
在CCD_ 1阶段期间,初始化所有的提供者并且执行所有的CCD_。config
部分可能包含配置提供者对象的代码,因此它们可以注入提供者对象。但是,由于提供程序是服务对象的工厂,并且在这个阶段提供程序没有完全初始化/配置->您不能在这个阶段要求提供程序为您创建服务->在配置阶段您不能使用/注入服务。当这个阶段完成时,所有的提供者都准备好了(在配置阶段完成后,不能再进行提供者配置)。
第2阶段:运行
在CCD_ 4阶段期间,执行所有的CCD_。在这个阶段提供者已经准备好,可以创建服务->在run
阶段,您可以使用/注入服务。
示例:
1.将$http
服务注入提供程序初始化函数将不起作用
//ERRONEOUS angular.module('myModule').provider('myProvider', function($http) { // SECTION 1: code to initialize/configure the PROVIDER goes here (executed during `config` phase) ... this.$get = function() { // code to initialize/configure the SERVICE goes here (executed during `run` stage) return myService; }; });
由于我们试图将$http
服务注入在config
阶段执行的函数中,因此我们将得到一个错误:
Uncaught Error: Unknown provider: $http from services
这个错误实际上表明,用于创建config
1服务的$httpProvider
还没有准备好(因为我们仍处于config
阶段)。
2.将$http
服务注入服务初始化函数将工作:
//OK
angular.module('myModule').provider('myProvider', function() {
// SECTION 1: code to initialize/configure the PROVIDER goes here (executed during `config` phase)
...
this.$get = function($http) {
// code to initialize/configure the SERVICE goes here (executed during `run` stage)
return myService;
};
});
由于我们现在将服务注入到服务初始化函数中,该函数在run
阶段执行,因此此代码将起作用。
这可能会给你一点优势:
var initInjector = angular.injector(['ng']);
var $http = initInjector.get('$http');
但要小心,成功/错误回调可能会使您在应用程序启动和服务器响应之间处于竞争状态。
这是一个老问题,如果我们想依赖库的核心功能,我们似乎有一些鸡蛋问题。
我所做的不是从根本上解决问题,而是绕过问题。创建一个包裹整个主体的指令。示例
<body ng-app="app">
<div mc-body>
Hello World
</div>
</body>
现在mc-body
需要在渲染之前初始化(一次),例如
link: function(scope, element, attrs) {
Auth.login().then() ...
}
Auth
是一种服务或提供商,例如
.provider('Auth', function() {
... keep your auth configurations
return {
$get: function($http) {
return {
login: function() {
... do something about the http
}
}
}
}
})
在我看来,我确实可以控制引导程序的顺序,这是在常规引导程序解析所有提供程序配置,然后尝试初始化mc-body
指令之后。
在我看来,这个指令可能领先于路由,因为路由也是通过一个指令(例如<ui-route />
)注入的。但我可能错了。需要更多的调查。
在回答您的问题"有什么想法吗?"时,我会回答"是"。但是等等,还有更多!
我建议在配置中使用JQuery。例如:
var app = angular.module('myApp', ['services']);
app.config(['$anyProvider', function ($anyProvider) {
$.ajax({
url: 'www.something.com/api/lolol',
success: function (result) {
$anyProvider.doSomething(result);
}
});
}]);
- $http.delete回调未在基本CRUD应用程序中启动
- Pinterest API应用程序处于开发模式(因此我可以使用http重定向)
- 我可以在React应用程序中使用什么作为Angular的等价物;s$http.get
- Angular提供程序和$http
- GWT应用程序中出现错误HTTP 404(“Script Tag Failure-no status available
- node.js-express.js API删除http后处理程序
- Angularjs提供程序:在返回之前解决$http请求
- 从Windows8JS应用程序获取仅HTTP cookie
- ES5上的Angular2 Http提供程序(不是TypeScript)
- 如何进行服务器端HTTP重定向更新引用程序(更改原始引用程序)
- 如何记录Apache Cordova应用程序中加载的iframe的http响应
- HTTP 请求后,数据未显示在 Ember 应用程序中
- 如何检查提供程序以查看 HTTP 数据是否已准备就绪
- 对于Chrome扩展程序,从https站点调用http站点时出现错误
- 如何检查“http://”是否包含在 Angular .js 应用程序的 url 输入中
- 区分对$http的应用程序调用和 Angular 对拦截器中静态资源的请求
- 为什么 HTTP 图像没有出现在 HTTPS 应用程序中
- $http AngularJS 应用程序初始化之前的请求
- 从 http 处理程序返回的 JSON 在 IE8 中为空,但在 IE10 或 Chrome 中不是
- ui.路由器不工作?这是我在plunker上的应用程序:http://plnkr.co/edit/JdRB2m0aWjl