2016-01-06 27 views
1

我想申請一個納克級來評價一個< p>標籤應用納克級來評價一個p標籤

<p <strong>LW$:</strong> {{d.lw_metric}} <strong>LW:</strong> {{d.lw_metric_percentage}} <strong>L4W:</strong> {{d.l4w_metric}}</p>    

我裏面的負值內部的不同表情有我的CSS:

.positive{ color: green} 
.negative{ color: red} 

但我不知道如何告訴角度評估所有值insi德標籤,而不是像這樣

ng-class = "{'positive':data.merchMetrics.LW_CHG_LY >=0,'negative':data.merchMetrics.LW_CHG_LY <0}" 

做逐一因爲我有這樣的標籤裏面有五名錶達,所以我想有一些方法來避免這種情況。

回答

1

你可以簡單地移邏輯控制器

ng-class="evaluateClass(data.merchMetrics)" 

控制器

$scope.evaluateClass = function(merchMetrics){ 
    var returnClass = ""; 
    //below for loop will iterate through each property of object 
    //and will decide the class need to apply 
    for (var key in merchMetrics) { 
     if (merchMetrics.hasOwnProperty(key)) { 
      //you could also add the logic here 
      //so that string will have positive & negative class once 
      if(merchMetrics[key] > 0) 
      returnClass = returnClass + "positive " 
      else 
      returnClass = returnClass + "negative " 
     } 
    } 
    //you could return your class from here 
    return returnClass; 
}; 
+0

大,非常感謝您的寶貴時間:) – kennechu

+0

@kennechu很高興地聽到,它helped..Thanks: ) –