Angular UI 路由器 $transitions.onBefore [英] Angular UI router $transitions.onBefore

查看:23
本文介绍了Angular UI 路由器 $transitions.onBefore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Angular ui 路由器版本 1.0.0-alpha.4,并且由于默认情况下根范围事件处于非活动状态,我想使用如下实现身份验证机制的新方法.

I am using angular ui router version 1.0.0-alpha.4 and since by default root scope events are inactive I thought of using new way of implementing auth mechanism as follows.

.run(['$transitions', '$timeout', '$q', '$state', 'AuthService', ($transitions, $timeout, $q, $state, AuthService) => {
  $transitions.onBefore({ to: 'app.*', from: '*' }, () => {
    const deferred = $q.defer();
    AuthService.isLoggedIn().then(() => {
      deferred.resolve()
    }, () => {
      // redirect to error page 
      // how can i redirect user to error page. $state.go('error') not working. 
      deferred.reject()
    });
    return deferred.promise;
  }, {priority: 10});
}])

但是问题是我不确定如何将用户重定向到错误页面.我尝试使用 $state.go('error') 并且似乎不起作用.有什么建议?

However issue is that I am not sure how to redirect user to error page. I try to use $state.go('error') and seems does not working. Any suggestions?

推荐答案

酷我找到了解决方案.将此发布给可能提出相同问题的任何人.您所需要做的就是按如下方式解决承诺.

Cool I found the solution. Posting this to anyone who may come up with the same issue. All you need is resolve the promise as follows.

deferred.resolve($state.target('error', undefined, { location: true }))

这篇关于Angular UI 路由器 $transitions.onBefore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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