我有下面的代碼在我驗證工廠:如何手動重新加載指令?
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 = '/'
時會這樣做,但是這樣做會擾亂我的測試。
我認爲解決方案是手動重新加載navbar指令。我怎樣才能做到這一點?這就是目前的指令看起來像:
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'對象 – user2718281