Angular UI路由器$ transitions.onBefore

fer*_*ndo 6 angularjs angular-ui-router

我使用角度ui路由器版本1.0.0-alpha.4,因为默认情况下根目录事件是非活动的我想到使用新的方式实现auth机制如下.

.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});
}])
Run Code Online (Sandbox Code Playgroud)

但问题是我不知道如何将用户重定向到错误页面.我尝试使用$ state.go('error')似乎无法正常工作.有什么建议?

fer*_*ndo 3

很酷,我找到了解决方案。将其发布给可能遇到相同问题的任何人。您所需要的只是按如下方式解决承诺。

deferred.resolve($state.target('error', undefined, { location: true }))
Run Code Online (Sandbox Code Playgroud)