Why AngularJS currency filter formats negative numbers with parenthesis?

后端 未结 7 795
别跟我提以往
别跟我提以往 2020-12-28 14:05

Live Demo

Why this:

# Controller
$scope.price = -10;

# View
{{ price | currency }}

results in ($10.00) rather than

7条回答
  •  没有蜡笔的小新
    2020-12-28 14:33

    It works better for me by checking negative number:

    var app = angular.module('myApp');
    
    app.filter('customCurrency', ["$filter", function ($filter) {       
        return function(amount, currencySymbol){
            var currency = $filter('currency');         
    
            if(amount < 0){
               return currency(amount, currencySymbol).replace("-", "(") + ')'
            }
    
            return currency(amount, currencySymbol);
        };
    }]);
    

提交回复
热议问题