Angular ngRoute conditional .when ()

I would like to implement a conditional .when () like:

.when('/abc', {
     // if MyService.allow == true
     template: '<myDirec></myDirec>'
     // else
     redirectTo: '/'
})

      

My route /abc

should be both "protected" by variable hold in one of my stateful services. So I want to set this state somewhere else to true / false and the next time the user tries to get to / abc it will be served conditionally.

How can I achieve this? - with a minimum number of third-party dependencies

What I have tried and read: - Just injecting my service into the .config I found out is not possible - Read about using the provider as they can be injected. But can I use them, how do I use my service? - template and templateUrl accept a function, but that didn't work for me

Thank you very much in advance!

+3


source to share


1 answer


You can use the $ routeChangeStart event. It runs when the url has changed and takes the current and next arguments:



$rootScope.$on('$routeChangeStart', function(event, next, current) {
  if (next === 'abc') // do something;
});

      

+3


source







All Articles