Tuesday 24 May 2016

the calling order in angular js

Here's the calling order:
  1. app.config()
  2. app.run()
  3. directive's compile functions (if they are found in the dom)
  4. app.controller()
  5. directive's link functions (again, if found)
Here's a simple demo where you can watch each one executing (and experiment if you'd like).
Example:
index.html
<div ng-app="myApp" ng-controller="myCtrl">
    <div test1 test2> </div>
</div>
index.js
var myApp = angular.module('myApp', []);
myApp.factory('aProvider', function() {
   console.log("factory");
});
myApp.directive("test1", function() {
    console.log("directive setup");
    return {
        compile: function() {console.log("directive compile");}
    }
});
myApp.directive("test2", function() {
    return {
        link: function() {console.log("directive link");}
    }
});
myApp.run(function() {
    console.log("app run");
});
myApp.config( function() {
    console.log("app config");
});
myApp.controller('myCtrl', function($scope) {
    console.log("app controller");
});

Order Of execution:
app config
VM61:64 app run
VM61:51 directive setup
VM61:53 directive compile
VM61:72 app controller
VM61:59 directive link

2 comments:

  1. Great information shared. Cryptex Technologies is one of the most well-known company for Ruby on Rails in India. If you need any help from us email at: info@cryptextechnologies.com

    ReplyDelete
  2. After reading this blog i very strong in this topics and this blog really helpful to all... explanation are very clear so very easy to understand... thanks a lot for sharing this blog on Ruby on Rails Online Training

    ReplyDelete