1

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?

4
  • what priority ? Commented Jun 4, 2017 at 22:38
  • A run block executed first if its priority is the biggest. Commented Jun 4, 2017 at 22:40
  • but what do you mean with priority of a run block? you can define priority on components/directives, but a run is executed simply when is found. Why you don't just structure your code in order to execute the one you want before the other one? Commented Jun 4, 2017 at 22:43
  • One of the run block are responsible to load configuration from server and init some basic services. Others are from modules and plugins. The first must be executed before the others. Commented Jun 5, 2017 at 6:22

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.