Javascript $scope未在更改时更新
我正在使用Satellizer进行身份验证,在我登录后,标题中的“登录/注册”按钮应更改为“注销”,但它们不会 这是我的头指令:Javascript $scope未在更改时更新,javascript,angularjs,angularjs-scope,angularjs-ng-show,satellizer,Javascript,Angularjs,Angularjs Scope,Angularjs Ng Show,Satellizer,我正在使用Satellizer进行身份验证,在我登录后,标题中的“登录/注册”按钮应更改为“注销”,但它们不会 这是我的头指令: angular.module('pages.components.navHeader', [ 'common.services.authenticationService' ]) .directive('navHeader', function () { var directive = {};
angular.module('pages.components.navHeader', [
'common.services.authenticationService'
])
.directive('navHeader', function () {
var directive = {};
directive.restrict = "E";
directive.templateUrl = "navHeader.tpl.html";
directive.controller = ['$scope','$location', 'CONFIG', 'authenticationService', function navHeaderCtrl($scope, $location, CONFIG, authenticationService) {
$scope.isAuthenticated = authenticationService.isAuthenticated();
$scope.logout = function (){
authenticationService.logout();
};
}];
return directive;
});
下面是我的标题模板的有趣部分:
<li ng-show="!isAuthenticated"><a ng-href="#">Login</a></li>
<li ng-show="!isAuthenticated"><a ng-href="#">Register</a></li>
<li ng-show="isAuthenticated"><a ng-click="logout()" href="#">Logout</a></li>
因此,即使单击“注销”,标题也不会更新。我在标题中设置isAuthenticated的方式是否有误 将控制器代码更改为
$scope.isAuthenticated = authenticationService.isAuthenticated;
然后在html上,您可以简单地使用
ng-show="isAuthenticated()"
注意
ng show
将调用authenticationService.isAuthenticated
方法在每个digest
周期
尝试添加
$scope.$apply()
在authenticationService.logout()方法之后添加$scope.isAuthenticated=authenticationService.isAuthenticated()代码>也在注销功能中
ng-show="isAuthenticated()"