我是angularjs的新手。重新加載AngularJS控制器
我的問題是,我有一個用戶控制器來處理登錄和註銷。我還有另一個控制器爲我的網站加載一個標題菜單。
如果用戶登錄到站點,我的isAuthenticated變量設置爲true。如果變量設置爲true,則標題應該改變,但是,我認爲必須重新加載控制器才能更改標題視圖。
這裏我HeaderController的代碼:
myapp.controller('HeaderController', ['$scope', '$location', '$window', 'AuthenticationService',
function HeaderController($scope, $location, $window, AuthenticationService) {
$scope.isAuthenticated = AuthenticationService.isAuthenticated;
if (AuthenticationService.isAuthenticated) {
$scope.user.vorname = $window.sessionStorage.user.vorname;
}
}
]);
這裏是我的HeaderDirective的代碼:
myapp.directive('appHeader', function() {
return {
restrict: 'E',
link: function(scope, element, attrs) {
if (attrs.isauthenticated == 'false') {
scope.headerUrl = 'views/header/index.html';
} else {
scope.headerUrl = 'views/header/isAuthenticated.html';
}
},
template: '<div ng-include="headerUrl"></div>'
}
});
我的index.html:
<div ng-controller="HeaderController">
<app-header isauthenticated="{{isAuthenticated}}"></app-header>
</div>
我怎樣才能重新加載控制器,如果用戶登錄到頁面?
PS:請原諒我發音不好。
謝謝,這就是我正在尋找的。 – BlackSalt 2014-10-03 08:13:05
不幸的是,這個答案有點失敗,每個人都在搜索如何重新加載控制器:(是的,在這種情況下不需要它,但還有其他用例(例如編寫基礎結構,可以重新初始化ui的位收到一個事件) – 2018-01-25 18:58:32
@GeorgeMauer我不明白爲什麼在你的用例中需要一個控制器reolad。你能給更多的上下文嗎?(也許最好打開一個新的SO問題作爲一個無效的方法您的需求) – 2018-01-25 19:07:48