我是一个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:请原谅我的发音不好。
$scope.$apply
。在桌面版的 Chrome 浏览器上它运行得很好。然而,在 Android 的 webview 上它并没有自动刷新。您使用$route
重新加载的技巧在 Android webview 4.4.2 上运行良好。 - nagu