2017-02-27 65 views
0

我有一種情況,我從多個產品的API中返回一個美元金額和折扣百分比。我有一個ng-repeat,在用戶界面中,我想顯示折扣金額。角度表達式中的計算

喜歡的東西:

{{this.amount * .(100 - this.discount)}} 

我嘗試這樣做,但它不工作。

有什麼建議嗎?

+0

是否同時定義了「this.amount」和「this.discount」?這裏使用'this'是很奇怪的,通常在角度上添加你想要的變量'$ scope'。例如。在你的控制器中:'$ scope.amount = 5',在你的視圖中:'{{amount}}'打印5. – thodic

回答

2

this.amountthis.discount定義?在這裏使用this很奇怪,通常在角度上添加你想要的變量$scope

E.g.

在你的控制器:$scope.amount = 5

那麼你的觀點:{{amount}}打印5.


括號之前忽略這一點的使用期限是造成問題,請嘗試:

{{this.amount * (100 - this.discount)/100}} 
+0

這樣做。謝謝! – cnak2

0

這是有效的語法:{{amount * 0.(100 - discount)}}(注意,0)

+0

嗨Mehul,我試過這個

{{item.cost * 0.(100 - item.discount)}}
,但它不起作用。這是我的ng-repeat:ng-repeat =「結果中的項目」 – cnak2

+0

@ cnak2你在控制檯中得到了什麼? – mehulmpt

0

刪除該關鍵字。相反,使用範圍變量來計算該值。

{{amount * .(100 - discount)}} 
+0

謝謝,Ashish。那只是一個例子。我實際上並沒有使用這個關鍵字。實際的行是:

{{item.cost * 0.(100 - item.discount)}}
cnak2