假設我顯示產品信息。我想如果價格低於50,那麼項目顏色應該是紅色。如果價格超過50,則物品顏色應爲黃色,如果價格超過(50+(50 * 60/100)),則物品顏色應爲綠色。現在告訴我,我怎麼能達到最好的方式。用最好的方法指導我完成它。如何在ng級中設置多個條件
這種方式我嘗試過,但我知道會有更多的迭代由於骯髒的檢查工作與ng重複。看到我的代碼,並告訴我怎麼可以在ng-class中設置多個條件來動態設置類。
<div ng-app="myApp">
<ul ng-controller="MyController">
<li ng-class="setColor(item.price)" ng-repeat="item in products">{{item.name}} — {{item.price}}</li>
</ul>
</div>
var myApp = angular.module('myApp', []);
myApp.controller('MyController', function MyController($scope) {
$scope.setColor = function(price) {
alert(price);
}
$scope.products = [
{
'name' : 'Xbox',
'clearance' : true,
'price' : 30.99,
},
{
'name' : 'Xbox 360',
'clearance' : false,
'salesStatus' : 'old',
'price' : 99.99,
},
{
'name' : 'Xbox One',
'salesStatus' : 'new',
'price' : 50,
},
{
'name' : 'PS2',
'clearance' : true,
'price' : 79.99,
},
{
'name' : 'PS3',
'salesStatus' : 'old',
'price' : 99.99,
},
{
'name' : 'PS4',
'salesStatus' : 'new',
'price' : 20.99,
}
]
})
告訴我沒有從ng-class調用函數我怎麼能在ng-class中放置多個if else條件?
if price > 50
return "css-class-yello"
else if price < 50
return "css-class-red"
else if price > (50+(50*60/100))
return "css-class-green"
如果可能請用代碼引導我。感謝
你的表達可以是控制器的功能,也可以爲每個類不同的表情。 – isherwood
看到我的最後一個if else,並告訴我如何在我的ng類中使用示例代碼的這種方式應用條件。 – Mou
請參閱Rob的回答。這就是我所掌握的。 :) – isherwood