Mostafa Barmshory
Mostafa Barmshory

Reputation: 2039

Priority of execution of runs in angularjs

Suppose there are two run function in app:

angular.module('exmple',[])
.run(function(){
    console.log('second');
})
.run(function(){
    console.log('first');
});

Is there any way to execute runs based on priority?

Upvotes: 1

Views: 241

Answers (1)

georgeawg
georgeawg

Reputation: 48968

If code is in a service, it will run in order of dependency:

angular.module('exmple',[])
.run(function(firstService){
    console.log('second');
})
.service("firstService", function(){
    console.log('first');
});

By having the run block define firstService as a dependency, the dependency injector will initialize that service before running the code of the run block.

The Demo

angular.module('app',[])
.run(function(firstService){
    console.log('second');
})
.service("firstService", function(){
    console.log('first');
});
<script src="//unpkg.com/angular/angular.js"></script>
<div ng-app="app">
</div>

Upvotes: 1

Related Questions