Search code examples
angularjsangular-filters

How to use multiple filter in $filter Angular JS


I have two filter which need to applied on the content of controller. First filter will be lowercase and other one is custom filter.

I tried to use it like follows:

$filter('lowercase','cardShortNameRegex')(currentProductObject.shortName);

Only lowercase filter get applied not the other one.

if some one have any solution or suggestion please help.


Solution

  • Just call it one after another.

    var lowerCasedName = $filter('lowercase')(currentProductObject.shortName);
    var result = $filter('cardShortNameRegex')(lowerCasedName);
    

    You can also define your own function something like this:

    var result = multipleFilters('lowercase', 'cardShortNameRegex')(currentProductObject.shortName);
    
    function multipleFilters() {
      var filters = arguments;
      return function (input) {
        angular.forEach(filters, function (filterName) {
          input = $filter(filterName)(input);
        });
        return input;
      }
    }
    

    which accepts multiple filter names and applies them.