2015-02-07 25 views
0

我正在使用實時應用程序來顯示當前黃金市場利率。我通過ajax獲得服務並將值轉換並顯示到表中。目前動態創建此表並附加到div。這樣就可以與錶行的數據進行比較與目前獲得價值,並可以設置價格上升/下降或正常顏色值像波紋管,如何在angularjs中綁定值之前獲取以前的值並與新值比較

var tableid = document.getElementById("ratetable"); 
for(var i=1;i<tableid.rows.length;i++) { 
    selling_rate = data.Commodities.Commodity.selling_rate; 
    buying_rate = data.Commodities.Commodity.buying_rate; 

    if(tableid.rows[i].cells[1].innerHTML > selling_rate) { 
     tableid.rows[i].cells[1].style.color = "#FFFFFF"; //Red 
     tableid.rows[i].cells[1].style.background = "#FF0000"; 
    } else if(tableid.rows[i].cells[1].innerHTML < selling_rate) { 
     tableid.rows[i].cells[1].style.color = "#ffffff"; //Green 
     tableid.rows[i].cells[1].style.background = "#2636f2"; 
    } else { 
     tableid.rows[i].cells[1].style.color = "#000000"; //black 
     tableid.rows[i].cells[1].style.background = ""; 
    } 
    tableid.rows[i].cells[1].innerHTML = selling_rate; 

} 

爲此,我使用波紋管代碼的角度JS創建表

<table width="480" border="0" cellpadding="0" cellspacing="0" class="rate-table" id="ratetable"> 
            <tr class="table-title"> 
             <td width="297">DESCRIPTION</td> 
             <td width="183">PRICE</td> 
            </tr> 
             <tr ng-repeat="commodity in commodityrate.Commodity" ng-class-odd="'oddrow'" ng-class-even="'silver'"> 
              <td width='297'>{{commodity.name}}</td> 
              <td width='183'>{{commodity.selling_rate}}</td> 
            </tr> 

          </table> 

但我需要檢查值(比較新值與以前的值)和更新顏色代碼。對於這個我試過像

ng-class="{highcolor: $scope.text>commodity.selling_rate, lowcolor: $scope.text<commodity.selling_rate, normalcolor: $scope.text==commodity.selling_rate}" 

但是這並沒有工作,如何做到這一點在角js。有沒有辦法做這個動態創建的價值來比較角度$ watch。請任何人幫助我。

回答

1

目前我沒有想出如何在括號表達式中存儲或獲取以前的值,但有一個解決方法:看這些元素的模型變化

而是每一個元素上加入了$watch的,你可以設置objectEquality爲真

scope.$watch('commodityrate.Commodity', function(newValue, oldValue) { 
    //iterating newValue and compare it the corresponding element in oldValue. 
}, 
true); 

BTW看你的商品清單commodityrate.Commodity,你仍然需要像priceTrend一個新的屬性添加到Commodity和使用定義您的ngClass

由於您在從後端獲取新價格時總是需要檢查每種商品的價格變化,因此,啓用objectEquality後,手錶commodityrate.Commodity不會導致性能下降。

+0

謝謝,它幫助我很多。 – 2015-02-07 06:44:45

相關問題