I am working on a login page, with a successful redirect to the home page. By default, I display the login page for this code:
app.run(function($rootScope, $location) { $rootScope.$on('$routeChangeSuccess', function() { $location.url("/login"); }); });
Then, after checking the user / password data from the backend, I take the user to the home page:
$scope.login = function() { if ($scope.username === 'admin' && $scope.password === 'pass') { console.log('successful') $rootScope.$on('$routeChangeSuccess', function() { $location.url("/home") }); $location.url("/blah"); } else { $scope.loginError = "Invalid username/password combination"; console.log('Login failed..') }; };
Forwarding does not work if I delete the second $location.url before the else section of the if . However, it does not use this url ( /blah ), it goes to home . but if url blah removed, the redirect logic does not work.
I cannot understand why I need to use two $location.url() . I would think, can someone help me understand how this redirect system works?
This may not be the best practice, I am open to suggestions on how to improve this, here is a Plunker Example
source share