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

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

问题描述

我的问题其实是非常相似的一个发现这里:

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

AngularJs - 取消路由变化事件

总之,我使用$ routeChangeStart并试图使用$位置来改变目前的路线。当我这样做,控制台显示我原来的网页仍然加载和很快被新的页面覆盖。

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,应为preventing额外的重定向工作。不幸的是,我使用了$ 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,我可以使用下一步和电流参数(请参见 AngularJS - $路线)作为对象来获取我的'访问'值。随着$ 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.

有什么办法,我可以用$ routeChangeStart的对象的灵活性结合起来$ locationChangeStart的重定向消息的制动力来实现我需要什么?

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

推荐答案

这想到的是试图用决心参数此办法一:

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' });

请注意,我没有测试code以上,但我在我的项目做类似的东西。

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天全站免登陆