Umar Syal Umar Syal - 3 months ago 8
AngularJS Question

Execute Angular code no matter what route user lands on

what I'm trying to do is write a piece of code in angular which runs against all of the routes.

generally the behavior in angular is that if I have a controller for the root route, it will run only when I go to that route. But I don't want this behavior. I want a piece of code to run no matter if the starting route user entered is example.com , example.com/abc , example.com/xyz or any other valid route.

well I'll try to be more clear...some piece of code should run whenever the client app runs. no matter which route it starts from

any help would be very kind

Answer

You can use the run block. Run blocks are like main method if you take in Java or other languages. A run block is the code which needs to run to kickstart the application.

var app = angular.module('App', ['']);

app.run(function() {
  //This will get execute after your config phase. 
});