2014-01-20 52 views
1

這裏是一個ngRepeat,它爲用戶重複Alerts。我想在一個時間段內逐一顯示每個警報。我正在使用ngShow和一個計數器變量$ scope.showedItem

模板

 <div class="container" > 
      <div ng-repeat="headerNotification in headerNotification.list" ng-show="{{showedItem==$index}}"> 
        <h3>{{headerNotification.getText()}}</h3> 
        <button ng-click="headerNotification.do()" class="btn btn-warning">{{headerNotification.getButtonText()}}</button> 
      </div> 
     </div> 

控制器

componentHeaderModule.controller('headerController', [ 
'$rootScope', 
'$scope', 
'componentHeaderNotification', 
'$interval', 
function ($rootScope, $scope, componentHeaderNotification, $interval) 
{ 
    ... 
    $scope.showedItem = 1; 

    $interval(function(){ 
     $scope.showedItem++; 
     if ($scope.showedItem > $scope.headerNotification.list.length) 
      $scope.showedItem = 0; 
    }, 5000); 
}]); 

當我在看DeveloperTool,我看到NG秀值改變每5秒,然而,查看不爽,同樣的警報一直在顯示。甚至,物品類沒有改變。

可能是什麼問題?我對雙向綁定的理解是一個很大的錯誤?

回答

1

ngShow評估的角度表達,所以你不希望它裏面的雙大括號。

所以裏面:

<div ng-repeat="headerNotification in headerNotification.list" ng-show="{{showedItem==$index}}"> 

改變這一點:

ng-show="{{showedItem==$index}}" 

這樣:

ng-show="showedItem==$index" 

角不,默認情況下,把$watch的屬性(這有助於一個很多與性能)。這就是爲什麼Angular希望你通過一個表達來評估。

當您在表達式中使用雙曲花括號時,將會評估捲曲內部的表達式,只有一次,當指令被鏈接並且結果(在您的情況下爲truefalse)被傳入並且永久使用,從來沒有changing-即使屬性的值發生變化,角從來沒有通知(因爲有上沒有$watch)。

舉例來說,如果showedItem等於0時,連接完成,那麼第一個ngRepeat項通過true而所有其他條目通過false - 導致你所看到的,在ngShow從未改變。

你想要表達式被評估每個消化週期,所以你通過表達英寸

+0

我不相信這一點!儘管ngShow值正在改變,它根本不會影響!我需要更加努力地學習它! – efirat

+1

一個關鍵是角度不會把屬性放在手錶上。那麼,什麼情況是,在鏈接時傳遞的屬性是什麼評價。現在你正在傳遞要麼TRUE;或'FALSE'。即使html正在改變,它也不會被重新評估。這就是爲什麼你要在鏈接傳遞表達。 – KayakDave

相關問題