gpt4 book ai didi

javascript - Angular JS |刷新浏览器时,angularjs 模板会在安全上下文之前加载。如何阻止它?

转载 作者:行者123 更新时间:2023-11-30 17:01:16 25 4
gpt4 key购买 nike

在我的 angularjs 应用程序(使用 angular-ui)中,刷新浏览器时,在从服务器获取安全上下文之前加载特定状态的模板。获取安全上下文的 $http 调用被命中,但由于它是异步的,因此尚未设置。

解决这个问题的最佳方法是什么?

此外,我一直在尝试执行以下操作:

//on the state change event from refresh

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
console.log("state change has started");
if(fromState.name === '' && fromState.url === '^'){
console.log("inside state change from refresh");
//what needs to be done in this block to let the security context is fetched (should I use $timeout etc.) ?
.........
.........
}
});

最佳答案

我们使用以下两个代码片段来解决这个问题。在我们结束这里之前,我们尝试了其他几种方式。这个好像还挺结实的……

在用户服务工厂中:

     // setup UserService OBject
...
// bootstrap the Service
var tokenRetrieved = $location.search().token || localStorageService.get(AUTH_COOKIE_NAME);

if (tokenRetrieved) {
UserService.login(tokenRetrieved, true)
.then(function success(user) {
UserService.initialized = true;
}, function error(err) {
localStorageService.remove(AUTH_COOKIE_NAME);
UserService.initialized = true;
});
} else {
UserService.initialized = true;
}

return UserService;

在 $stateChangeStart 处理程序中:

            if (UserService.initialized) {
// here we check authorization for the toState
....


} else {
// if the UserService is not done initializing we cancel the stateChange and schedule it again in 200ms
event.preventDefault();
$rootScope.$log.log('preventing state change, because UserService not ready to check Authorization');
$timeout(function () {
$state.go(toState, toParams);
}, 200);
}

关于javascript - Angular JS |刷新浏览器时,angularjs 模板会在安全上下文之前加载。如何阻止它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28807088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com