title: Delaying loading a partial + controller on page load/refresh
body: <p>I'm trying to delay loading other controllers until authentication has taken place in my MainCtrl.</p>
  
  <p>index.html:</p>
  
  <pre><code>&lt;body ng-app="MyApp" ng-controller="MainCtrl"&gt;
  ...
      &lt;div ng-view&gt;
          ...
      &lt;/div&gt;
  ...
  &lt;/body&gt;
  </code></pre>
  
  <p>MainCtrl:</p>
  
  <pre><code>var checkAuth = function () {
      var deferred = $q.defer();
  
      userAuth.isAuthenticated(
  
      function () {
          $rootScope.user = userAuth.user.data;
          deferred.resolve();
      }, function () {
          $rootScope.user = false;
          deferred.resolve();
      });
  
      return deferred.promise;
  };
  
  $scope.$watch('$location.path()', function () {
  
      $rootScope.$on('$locationChangeSuccess', function (event) {
          checkAuth();
  
      });
  });
  </code></pre>
  
  <p>What I want is for the checkAuth function to run and then if the user is authorised, to load a partial and controller.</p>
  
  <p>I have tried various solutions as suggested in similar questions (<a href="https://stackoverflow.com/questions/11972026/delaying-angularjs-route-change-until-model-loaded-to-prevent-flicker">Delaying AngularJS route change until model loaded to prevent flicker</a>, <a href="https://stackoverflow.com/questions/12356185/angular-js-delaying-controller-initialization">Angular.js delaying controller initialization</a>, <a href="https://stackoverflow.com/questions/15905547/angularjs-prevent-not-authenticated-user-from-accessing-given-routes">AngularJS - prevent not authenticated user from accessing given routes</a>) but <strong>the problem is that none of them work when the page is refreshed or if the user is arriving from an external page</strong>. I've tried to use $locationChangeSuccess to run the authentication on page load but the partial controller doesn't wait for the authentication to be run before loading.</p>
  
  <p>Can anyone suggest a better solution or where I might be going wrong with my approach?</p>