title: AngularJS: Understanding $rootScope.$on('$routeChangeSuccess
body: <p>I am working on a login page, on success, it redirect to home page. By default I show login page this code:</p>
  
  <pre><code>app.run(function($rootScope, $location) {
    $rootScope.$on('$routeChangeSuccess', function() {
        $location.url("/login");
    });
  });
  </code></pre>
  
  <p>Then after validating the user/pass details from the backend I take the user to the home page:</p>
  
  <pre><code>$scope.login = function() {
      if ($scope.username === 'admin' &amp;&amp; $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..')
      };
    };
  </code></pre>
  
  <p>The redirect doesn't seem to work if I remove the second <code>$location.url</code> before the <code>else</code> section of the <code>if</code> statement. It is however not using that url (<code>/blah</code>), it goes to <code>home</code>. but If url <code>blah</code> is removed it the redirect logic does not work.</p>
  
  <p>I can't seem to understand why I have to use two <code>$location.url()</code>. I would appretiate if someone can help me understand how this redirect system works?</p>
  
  <p>This might not be the best practice, I am open to suggestions on how to improve this, here is <a href="http://plnkr.co/edit/N0F0pM?p=preview">Plunker example</a></p>