ANGULARJS Module Loading

Source: Internet
Author: User
Configuration block

During the load phase of the module, Angularjs configures the module during provider registration and configuration. Throughout the ANGULARJS workflow, this phase is the only part that can be modified before the app is launched.

Angular.module (' myApp ', [])

. config (function ($provide) {});

The most commonly used factory,directive are the syntax sugars for the config function, which are executed during the configuration phase.

It is important to note that Angularjs executes them in the order in which they are written and registered. This means that we cannot inject a provider that is not yet registered. The only exception is the constant () method, which is always executed before all configuration blocks.

Run block

Unlike the configuration block, the run block is executed after the injector is created, which is the first method to be executed in all ANGULARJS applications, closer to the concept of the Main method. Code blocks in a running block are often difficult to unit test, and are highly coupled to the application itself. The run block is typically used to register the global event listener, for example, we set up a listener for routed events in the. Run () block and filter for unauthorized requests.

Suppose we need to execute a function to verify the user's permissions when each route changes, and the only logical place to place this function is the Run method.

Angular.module (' myApp ', [])

. Run (function ($rootScope, authservice{

$rootScope. $on (' $routeChangeStart ', function (evt, next, current) {

If not log in

if (! Authservice.userloggedin ()) {

if (Next.templateurl = = = "Login.html") {

Has turned to login route so no redirection is required

} else {

$location. Path ('/login ');

}

}

})

})

ANGULARJS Module Loading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.