简体   繁体   中英

Angular methods (constants, config, run, factory, service), Does it matter what order you call them?

I have an application built on generator-angular-fullstack and it does a great job of allowing all my angular components to live in their own separate files.

I was just running all my code through JSLint and it asked to remove 'use strict'; from my index.module.js file as it worked out that this was the global or starting file for my entire application.

I was reviewing the JSLint warning here use-the-function-form-of-use-strict

This got me thinking, how did JSLint know that index.module.js was the starting code block.

Which then got me thinking, does it matter what order angular startup methods are called.

Can these methods be run in any order you like, or is there an expected sequential order for these calls?

angular.module('appName')
angular.module('appName').run(function() { });
angular.module('appName').config(function() { });
angular.module('appName').service(function() { });
angular.module('appName').constant('blah', 'blah');

TL;DR - no it's not.

The way angular is doing it - is when the page renders and scripts are loaded it register all the components(services\\config\\constants..) but do not execute them. When the registration completes angular is starting to execute the application, providers --> config --> run and so on.. If your interested in some more detailed explanation on the way angular works under the hood you can check out this awesome article .

*forgot to mention that you must define your module first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM