It is easy to see the hierarchy of performance by playing with console.log(). As you can see in this snippet , the order of execution:
- => Run
- => Factory
- => Service
- => Provider
Run . main Angular.
AngularJS, . . console.
, JS Bin, , .
angular.module('App', []);
angular.module('App').config(function(){
console.log('1. Config: You cannot inject $rootScope here');
});
angular.module('App').run(function($rootScope){
console.log('2. Run: Close to a "main" method');
$rootScope.counter = 1;
$rootScope.components = [];
$rootScope.components.push({
'name': 'Run',
'order': $rootScope.counter
});
});
angular.module('App').controller('Ctrl', function($rootScope, Factory, Service, Provider) {
console.log('7. Controller: Set up the initial state & add behavior to $scope');
$rootScope.counter ++;
$rootScope.components.push({
'name': 'Controller',
'order': $rootScope.counter
});
});
angular.module('App').directive('directive', function($rootScope) {
console.log('3. Directive: Use to manipulate the DOM');
$rootScope.counter ++;
$rootScope.components.push({
'name': 'Directive',
'order': $rootScope.counter
});
return {
controller: function() {
console.log('* Directive controller');
},
compile: function(){
console.log('* Directive compile');
return {
pre: function(){
console.log('* Directive pre link');
},
post: function(){
console.log('* Directive post link');
}
};
}
};
});
angular.module('App').filter('low', function($rootScope) {
return function filterOutput(input) {
console.log('8. Filter: Use to format a value');
$rootScope.counter ++;
$rootScope.components.push({
'name': 'Filter',
'order': $rootScope.counter
});
return input.toLowerCase();
};
});
angular.module('App').factory('Factory', function($rootScope) {
console.log('4. Factory - before controller');
$rootScope.counter ++;
$rootScope.components.push({
'name': 'Factory',
'order': $rootScope.counter
});
return 'Factory';
});
angular.module('App').factory('Service', function($rootScope) {
console.log('5. Service - before controller');
$rootScope.counter ++;
$rootScope.components.push({
'name': 'Service',
'order': $rootScope.counter
});
return 'Service';
});
angular.module('App').factory('Provider', function($rootScope) {
console.log('6. Provider - before controller');
$rootScope.counter ++;
$rootScope.components.push({
'name': 'Provider',
'order': $rootScope.counter
});
return 'Provider';
});
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"></script>
<section ng-app="App" ng-controller="Ctrl" class="jumbotron">
<ul directive>
<li ng-repeat="item in components | orderBy:'order'">
<b>{{item.order}}</b> => {{item.name}}
</li>
</ul>
<p>A quick overview of {{'ANGULAR FLOW' | low}}.</p>
</section>