2016-03-24 52 views
0

我想要一個方式來爲HTML表格提供一個條件格式(一個簡單的背景顏色)與AngularJS控制器的結果。這將有希望發生在ng-repeat迭代期間,因爲它通過數組中的元素工作。ng-repeat內條件格式/樣式

我想完成的是:每當控制器內部的函數返回true時,背景或樣式是綠色的,當它不是時,它將是紅色的。

在插入點的註釋中尋找「開始問題」。

 <!-- PLAYER STATS TABLE --> 
    <div id = "StatsListTable"> 
    <table border="1" style="width:80%"> 
    <tr> <!-- TODO: class="tr.pstats" --> 
     <th>Character Name</th> 
     <th>  
      <div ng-app="KIdash" ng-controller="controller"> 
      Total Matches: {{TotalMatchesFunc()}} :: {{GreaterWins()}} 
      </div> 
     </th> 
     <th class="green">Wins</th> 
     <th class="red">Losses</th> 
    </tr> 
     <tr ng-repeat="ps in PlayerStats" class = "playerStatTable"> 
      <td>{{ps.name}}</td> 
      <td>{{ps.matches}}</td> 

      <!-- BEGIN QUESTION --> 
      <!-- IF {{GreaterWins()}} make it green --> 
      <!-- ELSE     make it red --> 
      <script type="text/javascript"> 
      function() { 
       if(controller.GreaterWins()) 
       { 
        <th class="green">{{ps.wins}}</th> 
       } else { 
        <th class="red">{{ps.wins}}</th> 
       } 
      } 
      </script> 
      <!-- END IF --> 
      <td>{{ps.losses}}</td> 
     </tr> 
    <table> 
    </div> 
    <!-- END PLAYER STATS TABLE --> 
+0

你爲什麼更新原始問題?我會說保持原來的問題不變,如果你想要的話,添加一個編輯解決方案。 –

回答

2

你可以用ng-class來做到這一點。像下面這樣:

<tr ng-repeat="ps in PlayerStats" class = "playerStatTable"> 
     <td>{{ps.name}}</td> 
     <td>{{ps.matches}}</td> 
     <td ng-class="{'green': GreaterWins(),'red': !GreaterWins() }">{{ps.wins}}</td>     
     <td>{{ps.losses}}</td> 
</tr> 
+0

什麼是優秀的答案!謝謝。我所有的結果都是紅色的,有什麼具體的我需要做,以確保GreaterWins()函數實際上被正確使用?我需要在此上下文中將其稱爲controller.GreaterWins()嗎? –

+0

你不應該明確寫'controller.GreaterWins()',因爲你沒有使用控制器作爲語法。在'controller'範圍內定義'GreaterWins()'funciton?並且它總是返回'假'? –

+0

當我用T或F替換邏輯>比較時,它的行爲與預期相同。所以看來我只是沒有正確比較這些值。 –