2016-11-03 67 views
0

我在模板上做了一些數學計算,有時結果是負數。 我的問題是,當結果是一個負數,它顯示一個括號包裝它。我該如何解決它? 使用Angular 1.2。角度顯示負數的括號

我的控制器:

    $scope.totalToReceive = totalToReceive; 
        $scope.discountedTotal = discount; 
        $scope.antecipatedTotal = antecipatedTotal; 
        $scope.totalReceived = totalToReceive - discount + antecipatedTotal; 

我的模板:

<li> 
    <span class="big-number"><span>R$</span> {{ totalReceived | currency: ""}}</span> 
</li> 

更新: 我試着寫我自己的過濾器,但它沒有工作

.filter('numeric', function(){ 
return function (value) { 
    if (value < 0) { 
     value = '-' + Math.abs(value) + ''; 
    } 
} 
}) 
+0

這就是[貨幣過濾器是]什麼(https://docs.angularjs.org/api/ng/filter/currency)..如果你想要不同的行爲,你需要編寫你自己的過濾器。 –

+0

@MikeMcCaughan我試過了。我會更新我的問題。 – vbotio

+0

剝離圓括號過濾器可以在這裏找到:http://stackoverflow.com/questions/31532393/angularjs-remove-currency-filter- paranthesis – Adrianopolis

回答

0

找到了解。非常簡單。只是所著一個新的過濾器:

.filter('customCurrency', ['$filter', function($filter){ 
return function (amount, currencySymbol) { 
    var currency = $filter('currency'); 

    if(amount < 0) { 
     return currency(amount, currencySymbol).replace("(", "-").replace(")", ""); 
    } 
} 
}]) 

模板:

<span class="big-number"><span>R$</span> {{ totalReceived | customCurrency: ""}}</span>