【发布时间】:2015-08-13 21:39:30
【问题描述】:
我的身份验证工厂中有以下代码:
login: function(user) {
return $http
.post('/login', user)
.then(function(response) {
Session.setUser(response.data);
$cookies.put('userId', response.data._id);
$state.go('home', { reload: true });
// $window.location.href = '/';
})
;
},
问题是我的导航栏没有更新(我在视图中有属性是数据绑定到vm.currentUser;见下文)。当我使用$window.location.href = '/' 时会这样,但这样做会弄乱我的测试。
我认为解决方案是手动重新加载导航栏指令。我怎样才能做到这一点?这是指令当前的样子:
angular
.module('mean-starter')
.directive('navbar', navbar)
;
function navbar() {
return {
restrict: 'E',
templateUrl: '/components/navbar/navbar.directive.html',
controller: NavbarController,
controllerAs: 'vm'
};
}
function NavbarController(Auth) {
var vm = this;
Auth
.getCurrentUser()
.then(function(currentUser) {
vm.currentUser = currentUser;
})
;
vm.logout = function() {
Auth.logout();
};
}
【问题讨论】:
-
当您登录时,您会使用
Session.setUser。你能显示这个用户与Auth.getCurrentUser()返回的用户的关系代码吗?您的NavbarController可能引用了不同的 'currentUser` 对象
标签: angularjs angular-directive