title: Redirecting to a certain route based on condition
body: <p>I'm writing a small AngularJS app that has a login view and a main view, configured like so:</p>
  
  <pre><code>$routeProvider
   .when('/main' , {templateUrl: 'partials/main.html',  controller: MainController})
   .when('/login', {templateUrl: 'partials/login.html', controller: LoginController})
   .otherwise({redirectTo: '/login'});
  </code></pre>
  
  <p>My LoginController checks the user/pass combination and sets a property on the $rootScope reflecting this:</p>
  
  <pre><code>function LoginController($scope, $location, $rootScope) {
   $scope.attemptLogin = function() {
     if ( $scope.username == $scope.password ) { // test
          $rootScope.loggedUser = $scope.username;
          $location.path( "/main" );
      } else {
          $scope.loginError = "Invalid user/pass.";
      }
  }
  </code></pre>
  
  <p>Everything works, but if I access <code>http://localhost/#/main</code> I end up bypassing the login screen. I wanted to write something like "whenever the route changes, if $rootScope.loggedUser is null then redirect to /login"</p>
  
  <p>...</p>
  
  <p>... wait. Can I listen to route changes somehow? I'll post this question anyway and keep looking.</p>