AngularJS - Need some combination of $routeChangeStart and $locationChangeStart

后端 未结 4 1762
夕颜
夕颜 2020-12-23 10:46

My problem is actually very similar to the one found here:

AngularJs - cancel route change event

In short, I\'m using $routeChangeStart and trying to change

4条回答
  •  鱼传尺愫
    2020-12-23 11:18

    One approach that comes to mind is trying to use the resolve parameter for this:

    var resolver = function(access) {
      return {
        load: function($q) {
          if (access) { // fire $routeChangeSuccess
            var deferred = $q.defer();
            deferred.resolve();
            return deferred.promise;
          } else { // fire $routeChangeError
            return $q.reject("/login");
          }
        }
      }
    }
    
    $routeProvider.
      when('/login', { controller: 'LoginCtrl', templateUrl: '/app/partial/login.html', resolve: resolver(false)}).
      when('/home', { controller: 'HomeCtrl', templateUrl: '/app/partial/home.html', resolve: resolver(true)}).
      otherwise({ redirectTo: '/login' });
    

    Please note that I haven't tested the code above but I'm doing similar stuff in my projects.

提交回复
热议问题