你能在其他之前运行 Angular 服务(或该服务上的函数)吗?理想情况下,一旦 ng-app
被解析。
这是我的用例:我正在编写一个应用程序,它从服务器获取 AJAX 数据,然后以一百种不同的方式解析数据。我想在调用所有 Controller 之前进行初始 AJAX 调用吗?这样我就可以将所有数据解析并加载到服务中,而无需担心更新任何 Controller 或其他任何内容。
最佳答案
I would like to make the initial AJAX call before all the controllers get called
在 Angular 方法中,run
在调用任何 Controller 之前触发
var app = angular.module('myApp',[]);
app.run(function($rootScope){
// ajax call and other stuff
}
在 run
方法中,您可以执行任何工作,例如登录 Facebook、 token 验证等
引用
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.
关于javascript - Angular.js - 在其他任何事情之前运行服务功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25804767/