使用引數建立過濾器

預設情況下,過濾器具有單個引數:應用它的變數。但是你可以將更多引數傳遞給函式:

angular
  .module('app', [])
  .controller('MyController', function($scope) {
    $scope.example = 0.098152;
  })
  .filter('percentage', function($filter) {
    return function (input, decimals) {
      return $filter('number')(input * 100, decimals) + ' %';
    };
  });

現在,你可以為 percentage 過濾器提供精度:

<span ng-controller="MyController">{{ example | percentage: 2 }}</span>
=> "9.81 %"

…但其他引數是可選的,你仍然可以使用預設過濾器:

<span ng-controller="MyController">{{ example | percentage }}</span>
=> "9.8152 %"