我想要实现的是,如果会话已从任何页面超时,我将用户重定向到登录页面,我想显示一些错误。现在我能够重定向,但错误部分无效。
这是我的httpInterceptor
@app.service 'httpInterceptor', [
'$window',
'AuthenticationService'
($window, AuthenticationService) ->
{
responseError: (res) ->
if res.status == 403
AuthenticationService.error = true;
$window.location.href = '/'
res
}
]
并且在登录控制器中我尝试使用此服务
@app.service 'AuthenticationService', ->
@error = false
return
AuthenticationService.error
但我仍然认为这个值是假的。
编辑JS代码
this.app.service('httpInterceptor', [
'$window',
'AuthenticationService',
($window, AuthenticationService) =>
({
responseError(res) {
if (res.status === 403) {
AuthenticationService.error = true;
$window.location.href = '/';
}
return res;
}
})
]);
服务
this.app.service('AuthenticationService', function() {
this.error = false;
});
答案 0 :(得分:0)
如果您正在使用locationRouter,请使用$ location服务;如果您使用stateProvider进行视图更改,请使用$ state服务。
示例 - $ location.path(' /'); $ state.go(' /&#39);
这将避免重置您为错误消息设置的服务值。
this.app.service('httpInterceptor', [
'$window',
'AuthenticationService','$location;, '$state',
($window, AuthenticationService) =>
({
responseError(res) {
if (res.status === 403) {
AuthenticationService.error = true;
//$window.location.href = '/'; // instead of this
// Use below
$state.go('/'); or $location.path('/');
}
return res;
}
})
]);
答案 1 :(得分:0)
执行$window.location.href = '/';
时,必须重新加载页面,因此刷新后会清除您设置为服务的所有内容。如果您确实不需要刷新页面,请按照建议选择$location
或$state
。如果您仍想重新加载页面,则需要在页面/状态之间路径信息。您可以使用localStorage
执行此操作,或者只需将查询参数添加到网址.href = '/?authError=Y'
,然后在app bootstrap上阅读+清理它。
更新:根据您的评论,您需要重定向到另一个页面,这不是角度页面?如果是这样,您既不能同时使用$location
也不能$state
,因为角度不知道如何解决这些问题(您可以执行$location.href = "\"
,但这与$window.location.href = '/';
相同)。
P.S。 cofeescript ...你是变态:-P