Tuesday, 23 June 2015

angular run


Module.run() function will be called only one time when your module get initialised, and it executed before module start to run.  Config() is another function which will be called before run().



  • Configuration blocks
     - 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 - 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.


  • angular.module('app', ["ss"]).
    config(function($logProvider) { // provider-injector
        // This is an example of config block.
        // You can have as many of these as you want.
        // You can only inject Providers (not instances)
        // into config blocks.
        console.log("config");

    }).
    run(function() { // instance-injector
        // This is an example of a run block.
        // You can have as many of these as you want.
        // You can only inject instances (not Providers)
        // into run blocks
        console.log("run");
    });


    angular.module('ss', []).
    config(function($logProvider) { // provider-injector
        // This is an example of config block.
        // You can have as many of these as you want.
        // You can only inject Providers (not instances)
        // into config blocks.
        console.log("ss config");

    }).
    run(function() { // instance-injector
        // This is an example of a run block.
        // You can have as many of these as you want.
        // You can only inject instances (not Providers)
        // into run blocks
        console.log("ss run");
    });

    The result is:

    demo.js:78 ss config
    demo.js:61 config
    demo.js:86 ss run
    demo.js:69 run







    No comments:

    Post a comment