微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐