Want to redirect to a certain route based on condition? After some diving through some documentation and source code, I think I got it working. Perhaps this will be useful for someone else.
I added the following to my module configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | angular.module(...) .config( ['$routeProvider', function($routeProvider) {...}] ) .run( function($rootScope, $location) { // register listener to watch route changes $rootScope.$on( "$routeChangeStart", function(event, next, current) { if ( $rootScope.loggedUser == null ) { // no logged user, we should be going to #login if ( next.templateUrl != "partials/login.html" ) { // not going to #login, we should redirect now $location.path( "/login" ); } } }); }) |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.