Why AngularJS currency filter formats negative numbers with parenthesis?

后端 未结 7 716
别跟我提以往
别跟我提以往 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:26

    I know this is an old question, but the accepted answer is only answering why this happens, without a concrete solution to the problem. I think the "most correct way" of doing this, is to use a decorator like so:

    angular
        .module('app')
        .config(['$provide', function($provide) {
            $provide.decorator('$locale', ['$delegate', function($delegate) {
              if($delegate.id == 'en-us') {
                $delegate.NUMBER_FORMATS.PATTERNS[1].negPre = '-\u00A4';
                $delegate.NUMBER_FORMATS.PATTERNS[1].negSuf = '';
              }
              return $delegate;
            }]);
          }]);
    

    This is only called once, valid for any filter which depends on it, and you don't need a custom filter for your currency formatting.

提交回复
热议问题