在我的应用程序(用 yeoman 生成)中,我有这样的结构:
看法:
index.html
用于ng-view
渲染控制器视图
控制器:
每个控制器都尝试使用 api urlm 获取一些数据,例如:
$scope.getArticles = function() {
$http.get(settings.apiBaseUri + '/app/articles/', {
headers: {
'Content-Type': 'application/json',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'If-Modified-Since': ''
}
})
.success(function(response) {
/* do some magic here :) */
});
};
$scope.getArticles();
所以在每个控制器中(+许多其他查询)
现在我有点困惑:如何不渲染任何东西,直到我的第一个请求得到 200 而不是 401?
这是我的拦截器:
var deleteAuth = function(){
$location.path('/signin');
delete $localStorage.authStatus;
};
var responseError = function(rejection) {
...
if (rejection.status === 401 || rejection.status === 403) {
authStatus = $localStorage.authStatus;
if (authStatus && authStatus.isAuth && authStatus.authToken && !ipCookie('authToken') && !renewTokenAttempt) {
deleteAuth();
}
}
...
return $q.reject(rejection);
};
所有的作品几乎都是“必须的”,但是......
每次,我都会加载我的 index.html(带有侧边栏、徽标等),然后我才尝试获取(例如)我的文章,然后,如果我得到 401,我会被重定向到auth-page
,这不太好:因为我看到所有样式的小闪烁,然后才登录页面。
是真的吗,首先检查,如果我得到 200,然后才使用侧边栏等渲染页面,如果不是:然后是 auth-page。
怎么做?
我在网上看到了一些解决方案,但它们都很大,我需要一些非常快速和简单的东西)