Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $scope未在更改时更新_Javascript_Angularjs_Angularjs Scope_Angularjs Ng Show_Satellizer - Fatal编程技术网

Javascript $scope未在更改时更新

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 = {};

我正在使用Satellizer进行身份验证,在我登录后,标题中的“登录/注册”按钮应更改为“注销”,但它们不会

这是我的头指令:

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()"