你能在其他任何事情之前运行Angular服务(或该服务的功能)吗?理想情况下,只要ng-app被解析.
这是我的用例:我正在编写一个从服务器获取AJAX数据的应用程序,然后以一百种不同的方式解析数据.我想在调用所有控制器之前进行初始的AJAX调用?这样我只需要解析并加载服务中的所有数据,而不用担心更新任何控制器或其他什么.
解决方法:
I would like to make the initial AJAX call before all the controllers get called
var app = angular.module('myApp',[]);
app.run(function($rootScope){
// ajax call and other stuff
}
在run方法中,您可以执行任何工作,如登录Facebook,令牌验证等
参考
Configuration blocks (aka app.config) – get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.
Run blocks (aka app.run) – get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.