javascript - Angular.js - 在其他任何事情之前运行服务功能?

标签 javascript ajax angularjs

你能在其他之前运行 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.

docs.angularjs.org/guide/module

关于javascript - Angular.js - 在其他任何事情之前运行服务功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25804767/

相关文章:

javascript - 哪个是最安全的内存泄漏方法

javascript - ChartJS 后台多个图表

javascript - 对 Angular JS 中失败的日期应用过滤器

javascript - 如何检查数字是否大于 ng-change 上的数字

javascript - 使 div 像表格一样排序

javascript - 动态导入开启。 ("click", ...) 找不到全局变量

android - jQuery Mobile 未运行辅助 javascript

html - 使用 Json 获取未知路径

javascript - .one 和回调

javascript - 将 Unicode 转换为 UTF8