我正在构建一个MEAN堆栈应用程序,使用JWT和Passport进行身份验证,我能够在登录时将令牌保存在本地存储上并在Logout上将其删除,这很有效,但Angular App的状态如何用户登录时不更新状态。
更新状态的唯一方法是进行手动刷新。
我已经尝试了state.reload();
,$state.go('.', {}, { reload: true });
以及其中的所有内容,唯一可行的是$window.location.href = '/';
,但这没有任何意义,因为我想使用ui-router状态。
是否有强制摘要周期或更新状态的东西?
这是我所拥有的代码片段:
vm.doLogin = function() {
vm.formError = '';
authentication
.login(vm.credentials)
.error(function(err) {
vm.formError = err.message;
})
.then(function() {
$state.go('.', {}, { reload: true });
});
};
我真的很感激一些帮助。
答案 0 :(得分:0)
您是否尝试使用$state.go($state.current, {}, {reload: true});
或者您可以使用 $ route.reload(); 来重新初始化您的控制器
var currentPageTemplate = $route.current.templateUrl;
$templateCache.remove(currentPageTemplate);
$route.reload();
模板被缓存。如果这是你想要遵循的方式,那么你需要在调用reload之前从$ templateCache中删除模板。
另一个选项是在您登录后的视图中使用cache-view = false,如果您在该特定视图中维护动态状态时遇到问题。
$window.location.reload(); //reinitializes the controller as well as the services however, forcing the hard reload is bad.
答案 1 :(得分:0)
"home" is my abstract state which will host my modules.
"module1"&"module2" are two modules within "home"
State definitions as follows.
$stateProvider.state('login', {
url: "/login",
templateUrl: "app/login/login.html",
controller: 'LoginController'
})
.state('home', {
url: "/home",
abstract: true,
templateUrl: "<div ui-view></div>"
})
.state('home.module1', {
url: "/module1home",
templateUrl: "app/module1/module1.home.html",
controller: 'Module1HomeController'
})
.state('home.module2', {
url: "/module2home",
templateUrl: "app/module2/module2.home.html",
controller: 'Module2HomeController'
});
After successful login use the below code to navigate to "/home/module1home"(which ever default state's url) which in turn trigger the state.
$location.path('/home/module1home');