2014-09-01 107 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