1.背景介绍
2.知识剖析
3.常见问题
4.解决方案
5.编码实战
6.扩展思考
7.参考文献
8.更多讨论
the configuration blocks (config functions) are executed. This means the configuration blocks of the required modules execute before the configuration blocks of any requiring module.
Registration in the config block While it is recommended to register injectables directly with the module API, it is also possible to register services, directives etc. by injecting $provide or the individual service providers into the config function:
执行配置块(配置功能)。这意味着所需模块的配置块在任何需求模块的配置块之前执行。 在配置块中注册 .虽然建议直接使用模块API注册注入,但也可以通过将$ provide或各个服务提供者注入config函数来注册服务,指令等 .
angular.module('myModule', []). value('a', 123). factory('a', function() { return 123; }). directive('directiveName', ...). filter('filterName', ...); // is same as angular.module('myModule', []). config(function($provide, $compileProvider, $filterProvider) { $provide.value('a', 123); $provide.factory('a', function() { return 123; }); $compileProvider.directive('directiveName', ...); $filterProvider.register('filterName', ...); });
Run blocks are the closest thing in AngularJS to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the services have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test, and for this reason should be declared in isolated modules, so that they can be ignored in the unit-tests.
运行块是AngularJS中与main方法最接近的东西。运行块是需要运行以启动应用程序的代码。 在配置完所有服务并创建注入器后执行。运行块通常包含难以进行单元测试的代码, 因此应在隔离模块中声明,以便在单元测试中忽略它们。
main方法是一个特殊的方法,是程序执行的入口,一个Java程序从main方法开始执行。
AngularJS分两个阶段运行你的应用 – config阶段和run阶段。config阶段是你设置任何的provider的阶段, 它也是你设置任何的指令,控制器,过滤器以及其它东西的阶段。在run阶段,AngularJS会编译你的DOM并启动你的应用。 首先是配置块 通过config方法实现对模块的配置,AngularJS中的服务多数都对应一个provider, 用来执行与对应服务相同的功能或对其配置,比如$log、$http、$location都是内置服务, 相对应的“provider”分别是$logProvider、$httpProvider、$locationPorvider。 然后是运行块和配置块不同,运行块在注入器创建之后被执行,它是所有AngularJS应用中第一个被执行的方法。
我们如何在我们的应用程序中使用config()块?
我们如何在应用程序中使用run()块?
angular.module('crazydeveloperModule', []) .config(['$httpProvider','$locationProvider','$routeProvider','$provider','cookieProvider','constant', function ($httpProvider,$locationProvider,$routeProvider,$provider,cookieProvider,constant) { //这是配置块的一个例子。 //你可以根据需要拥有尽可能多的这些。 //你只能注入提供者(不是实例) //进入配置块。 }])
angular.module('myModule', []) . run(['$rootscope','security','pageFactory','$httpBackend', function ($rootscope,security,pageFactory,$httpBackend) { //实例注入器 //这是一个运行块的例子。 //你可以根据需要拥有尽可能多的这些。 //你只能注入实例(不是提供者) //进入运行块 }]);
$provide服务负责告诉Angular如何创造一个新的可注入的东西:即服务。 服务会被叫做供应商的东西来定义,你可以使用$provide来创建一个供应商。 你需要使用$provide中的provider()方法来定义一个供应商, 同时你也可以通过要求$provide被注入到一个应用的config函数中来获得$provide服务。
执行顺序不同,注入服务类型不同
配置块 - 此块在提供程序注册和配置阶段执行。只有提供程序和常量才能注入配置块。 此块用于注入模块方式的配置设置,以防止在完全配置服务之前意外实例化服务。使用config()方法创建此块。 运行块 - 在配置块之后执行此块。它用于注入实例和常量。使用run()方法创建此块。此方法类似于C或C ++中的主要方法。 运行块是放置需要在应用程序的根级别执行的事件处理程序的好地方。例如,身份验证处理程序。
参考资料http://www.cnblogs.com/Essence/p/4266618.html
参考资料:https://www.jb51.net/article/42492.htm