1

我正在使用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> 

我有一个工厂在情况satellizer额外的一层为不工作我,这只是一个指令,这样做:

.factory ('authenticationService', ['CONFIG', '$auth', function authenticationService (CONFIG, $auth) { 
    var authentication = {}; 

    authentication.isAuthenticated = function(){ 
     return $auth.isAuthenticated(); 
    }; 
    return authentication; 
}]) 

所以,即使当我点击注销,吨他的头不更新。我是否在我的头文件中设置isAuthenticated时出错?

+0

尝试增加'$范围$适用();'了'authenticationService.logout后() '方法 –

+0

在你的注销函数中添加'$ scope.isAuthenticated = authenticationService.isAuthenticated();' –

回答

2

更改控制器代码这个

$scope.isAuthenticated = authenticationService.isAuthenticated; 

,然后在HTML中你可以简单地使用。

ng-show="isAuthenticated()" 

注意

ng-show将调用authenticationService.isAuthenticated方法对每个digest周期