AngularJS - using multiple filters in a controller

I want to use several filters in the controller

Currently used

$filter('limitTo')($filter('lowercase')($filter('translate')('ACTIVE')), 5) 

If we have more of these filters. How can I use multiple filters in a standard controller format?

+5
source share
2 answers

You can simply enter the variables:

 var limitTo = $filter('limitTo'); var lowercase = $filter('lowercase'); var translate = $filter('translate'); var filteredValue = limitTo(lowercase(translate('ACTIVE')), 5); 

Or even

 var lowercaseStatus = lowercase(translate('ACTIVE')); var filteredValue = limitTo(lowercaseStatus, 5); 

Another strategy would be to use the same syntax as in the view:

 var filteredValue = $scope.$eval('"ACTIVE" | translate | lowercase | limitTo:5'); 
+2
source

This is an interesting question. Usually you do something like this or something like this:

 var translatedValue = $filter('translate')('ACTIVE'); var lowercaseValue = $filter('lowercase')(translatedValue); $scope.finalValue = $filter('limitTo')(lowercaseValue, 5) 

I created a service inspired by this answer .

 app.service('FilterChain', ['$filter', function($filter) { var chain = { value : '', start : function(value) { this.value = value; return this; }, applyFilter : function(filterName, args) { args = args || []; args.unshift(this.value); this.value = $filter(filterName).apply(undefined, args) return this; } }; return chain; }]); 

Use of this type

 $scope.value = FilterChain.start('Active') .applyFilter('translate') .applyFilter('limitTo', [5]) .applyFilter('uppercase') .value; 

You can use this service with other filters and objects, such as arrays. See a working example here: JSFiddle

+1
source

All Articles