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

你能在其他任何事情之前运行Angular服务(或该服务的功能)吗?理想情况下,只要ng-app被解析.

这是我的用例:我正在编写一个从服务器获取AJAX数据的应用程序,然后以一百种不同的方式解析数据.我想在调用所有控制器之前进行初始的AJAX调用?这样我只需要解析并加载服务中的所有数据,而不用担心更新任何控制器或其他什么.

解决方法:

I would like to make the initial AJAX call before all the controllers get called

在Angular方法中,在调用任何控制器之前触发run

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.

docs.angularjs.org/guide/module

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...