2014-09-01 112 views
2

我有一个基于身份验证的登录页面,我显示/隐藏div。 Div使用$ scope.userdetails来显示或隐藏。ng-show没有正确更新

<table class="txt_logged" ng-controller="mainCtrl" ng-show="{{userdetails}}"> 

下面是控制器手表方法

.controller('mainCtrl',['$scope','SharedService',function($scope,SharedService) 
{ 
    $scope.userdetails=SharedService.getUserDetails(); 

    $scope.$watch(function(){ return SharedService.getUserDetails()}, function(newValue, oldValue) 
    { 
     if (newValue && newValue !== oldValue) 
     { 
      var t= SharedService.getUserDetails(); 
      if(t.toString()=="true") 
      { 
       $scope.userdetails =true; 
      } 
      else 
      { 
       $scope.userdetails =false; 
      } 

     } 
    }); 

}]); 

为什么NG-显示没有更新

回答

2

你应该使用它像:

<table class="txt_logged" ng-controller="mainCtrl" ng-show="userdetails"> 

ngShow评估里面的表达属性。如果您输入值{{ userdetails }},它将评估为truefalse。这会尝试在$scope内搜索名为truefalse的变量。其原因评价内部ngShow表达是,这样就可以使用表达式,而不创建单独的变量的或逻辑,用于显示元件是过于复杂:

<table class="txt_logged" ng-controller="mainCtrl" ng-show="xyz == 'true' && userdetails"> 

还检查了:ngShow & ngHide

+2

感谢它的工作。你可以解释为什么{{userdetails}}不起作用。 – bharath 2014-09-01 07:27:41