2014-10-16 60 views
0

我哪裏行使用AngularJS ngRpeat生成的表格:如果我有什麼適用ngClass到ngRepeat聲明元素

$scope.divison = [ 
    { 
    name: "John Doe", 
    score: "10", 
    goingUp : true 
    }, 
    { 
    name: "Bob Marley", 
    score: "20" 
    } 
]; 

現在:

<tr ng-repeat="player in division"> 
    <td>{{player.name}}</td> 
    <td>{{player.score}}</td> 
</tr> 

數組看起來有點像這樣想要基於特定的ng-repeatng-class應用於表格行?我會雖然這可能會工作:

<tr ng-repeat="player in division" ng-class="'goingUp' : player.goingUp"> 
    <td>{{player.name}}</td> 
    <td>{{player.score}}</td> 
</tr> 

唉這是行不通的。可能是因爲ng級不在這個重複元素之內。我怎樣才能得到這個工作?

+0

嘗試:'ngclass =」 (player.goingUp)?'goingUp':''「' – 2014-10-16 13:11:52

+1

ng-class =」{'goingUp':player.goingUp}「 – Second2None 2014-10-16 13:17:41

+0

@ Second2None謝謝,我只是想念那些括號。如果你想發佈這個答案,我會接受它作爲正確的語法和最簡單的方法。 – Coop 2014-10-16 13:22:36

回答

1

正確的語法是

ng-class="{'goingUp' : player.goingUp}" 
0

它應該能夠這樣做,就像在this的情況下一樣。你可以在你的控制器的範圍,返回相應的類編寫一個函數:

$scope.isGoingUp = function(player) 
{ 
    if (player.goingUp) return "goingUp"; 
    return "notGoingUp"; 
} 

,然後調用它像這樣

ng-class="isGoingUp(player)" 
+0

感謝您的回答。雖然這種方法可能會起作用,但簡單地加入括號可以爲我工作,而且更容易。 – Coop 2014-10-16 13:34:47