2016-11-03 74 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>