Call function for each route in $routeProvider

Viewed 1962

I have got a little challenge and didn't find the solution yet. As I tried serveral things I'm asking your help so probably someone can lead me to the right direction.

I've got a routes.js in AngularJS. There am I using a $routeProvider. In each .when() I need to call a function (as I need to do something with the name).

$routeProvider
.when('/url', { templateUrl: '', /* callMyFunction('/url'); */ })
.when('/url1', { templateUrl: '', /* callMyFunction('/url'); */ })
.when('/url2', { templateUrl: '', /* callMyFunction('/url'); */ }) ....

This is just an example, I know this won't work. But to give you an idea what I'm trying to do. As you can see this will mess up my code (you need much more to get this working, so you'll get a lot of extra code for each .when() and this code is in every statement exactly the same (except for the argument).

So I'm looking for some function that can be called before or after (doesn't make sense as long as it's getting called) .when().

I've read plenty of questions here on Stack Overflow but didn't find the right answer. Most questions answered to put this in the controller. How ever, this function I've to call is for each route exactly the same. Because of this I don't want to put it in all my controllers but just in the routes.js and called for/after every when().

2 Answers
Related