title: On $routeChangeStart location doesn't find the corresponding template
body: <p>I have a web application and an <code>app</code> variable with the module <code>myApp</code>.<br>
  I have 3 pages: <code>/login</code>, <code>/registration</code> and <code>/</code>. My <code>$routeChangeStart</code> is defined to behave like follows:  </p>
  
  <ol>
  <li>Check if a global variable <code>user</code> is defined, if yes go to step <code>4</code></li>
  <li>Check where the user wants to go: if he doesn't want to go in <code>/login</code> or <code>/registration</code> do an <code>event.preventDefault()</code> and check if there is a valid session (via cookie) and try to authorize the user. If the user is authorized set the <code>user</code> global variable and redirect him to the page he wanted, otherwise send him to the <code>/login</code> page.</li>
  <li>If he wants to go to either on <code>/login</code> or <code>/registration</code> do nothing (we are in the case no user variable and I want to login or register).</li>
  <li>Check where the user wants to go: if he wants to go in <code>/login</code> or <code>/registration</code> do an <code>event.preventDefault()</code> and redirect him to the home <code>/</code> page (the user has a valid session, no need to login again).</li>
  </ol>
  
  <p>The logic works well, until we hit <code>F5</code> (I know it wipes the <code>$rootScope</code>, but at the cost of an extra AJAX call we should be fine with it) while in the <code>/</code> page with a valid session cookie. The application simply doesn't redirect to the correct template, the page remains blank. If we instead insert in the URL anything beside <code>/</code> (e.g. <code>/blah</code>) everything works fine. </p>
  
  <p>Here's my <code>app.js</code>:</p>
  
  <pre><code>(function () {
      angular.module("myApp", ["controllers", "services", "directives", "ngRoute"])
          .config(["$httpProvider", "$routeProvider", function ($httpProvider, $routeProvider) {
              $httpProvider.defaults.withCredentials = true;
              $httpProvider.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
  
              $routeProvider
                  .when("/login", {
                      controller: "LoginController",
                      templateUrl: "app/partials/login.html"
                  })
                  .when("/registration", {
                      controller: "RegistrationController",
                      templateUrl: "app/partials/registration.html"
                  })
                  .when("/", {
                      controller: "MainController",
                      templateUrl: "app/partials/home.html"
                  })
                  .otherwise("/");
  
          }])
          .run(function ($rootScope, $location, $http) {
              $rootScope.$on('$routeChangeStart', function (event, next, current) {
  
                  if (!$rootScope.user) {
  
                      if (next &amp;&amp; next.$$route &amp;&amp; (next.$$route.originalPath !== "/login" &amp;&amp; next.$$route.originalPath !== "/registration")) {
                          event.preventDefault();
  
                          $http.get("http://localhost:8080/myapp/api/user?self=true")
                              .success(function (data) {
                                  $rootScope.user = data;
                                  $location.path(next.$$route.originalPath);
                              })
                              .error(function () {
                                  $location.path("/login");
                              });
                      }
                  }else{
                      if (next &amp;&amp; next.$$route &amp;&amp; (next.$$route.originalPath === "/login" || next.$$route.originalPath === "/registration")){
                          event.preventDefault();
                          $location.path("/");
                      }
                  }
              });
          });
  
      angular.module("controllers", []);
      angular.module("services", []);
      angular.module("directives", [])
  })();
  </code></pre>
  
  <p>Why doesn't the <code>$location.path(next.$$route.originalPath);</code> in the <code>.success()</code> AJAX call work? Note that if I substitute it with e.g. <code>$location.path("/blah");</code> it is redirected correctly (but still doesn't work with <code>$location.path("/");</code>).</p>