AngularJS - 需要 $routeChangeStart 和 $locationChangeStart 的某种组合 [英] AngularJS - Need some combination of $routeChangeStart and $locationChangeStart

查看:27
本文介绍了AngularJS - 需要 $routeChangeStart 和 $locationChangeStart 的某种组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题实际上与此处发现的问题非常相似:

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

AngularJs - 取消路由更改事件

简而言之,我正在使用 $routeChangeStart 并尝试使用 $location 更改当前路线.当我这样做时,控制台会告诉我原始页面仍在加载并很快被新页面覆盖.

In short, I'm using $routeChangeStart and trying to change the current route using $location. When I do, the console shows me that the original page is still loads and is quickly overwritten by the new page.

提供的解决方案是使用 $locationChangeStart 而不是 $routeChangeStart,这应该可以防止额外的重定向.不幸的是,我在更改路线时需要访问 $routeprovider 中的其他数据(我用它来跟踪页面限制).这是一个例子...

The solution provided was to use $locationChangeStart instead of $routeChangeStart, which should work for preventing the extra redirect. Unfortunately, I'm using additional data in the $routeprovider that I need to access while changing the route (I use it to track page restrictions). Here's an example...

$routeProvider.
    when('/login', { controller: 'LoginCtrl', templateUrl: '/app/partial/login.html', access: false}).
    when('/home', { controller: 'HomeCtrl', templateUrl: '/app/partial/home.html', access: true}).
    otherwise({ redirectTo: '/login' });


$rootScope.$on('$routeChangeStart', function(event, next, current) {
    if(next.access){
        //Do Stuff
    }
    else{
        $location.path("/login");
        //This will load the current route first (ie: '/home'), and then
        //redirect the user to the correct 'login' route.
    }
});

通过 $routeChangeStart,我可以使用next"和current"参数(参见 AngularJS - $route) 作为对象来检索我的访问"值.使用 $locationChangeStart,这两个参数返回 url 字符串,而不是对象.所以似乎没有办法检索我的访问"值.

With $routeChangeStart, I can use the "next" and "current" parameters (see AngularJS - $route) as objects to retrieve my 'access' values. With $locationChangeStart, those two parameters return url strings, not objects. So there seems to be no way to retrieve my 'access' values.

有什么方法可以将 $locationChangeStart 的重定向阻止能力与 $routeChangeStart 的对象灵活性结合起来来实现我的需要?

Is there any way I can combine the redirect-stopping power of $locationChangeStart with the object-flexibility of $routeChangeStart to achieve what I need?

推荐答案

想到的一种方法是尝试为此使用 resolve 参数:

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.

这篇关于AngularJS - 需要 $routeChangeStart 和 $locationChangeStart 的某种组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆