Say I create an Angular app and write some new filters: Show only odd
s, and show only lucky
numbers.
There's an oddList
filter and a luckyList
filter:
var app = angular.module('app', []);
app.filter('oddList', function() {
return function(items) {
var filtered = [];
angular.forEach(items, function(item) {
if (item % 2 !== 0)
filtered.push(item);
});
return filtered;
};
});
app.filter('luckyList', function() {
return function(items) {
var filtered = [];
angular.forEach(items, function(item) {
if (item === 2 || item === 7 || item === 11)
filtered.push(item);
});
return filtered;
};
});
In the view portion, I can chain these filters:
<ul><li ng-repeat="number in numbers | oddList | luckyList">{$number}</li></ul>
When this works, I should see 7
and 11
remaining.
I want to make my filters variable for this ng-repeat
. Is there something I can do that's similar to this process?
Say I name the variable filter filter:listFilter
so that in our controller, we can dynamically update $scope.listFilter
.
<ul><li ng-repeat="number in numbers | filter:listFilter">{$number}</li></ul>
And the controller with pseudo code:
app.controller('main', function($scope, $filter) {
$scope.numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
$scope.listFilter = $filter('oddList | luckyList');
});
Any idea how I could chain (variable) filters in the controller? Would like to be able to toggle between just odd, just lucky and/or both.
Using filters in view templates Filters can be applied to the result of another filter. This is called "chaining" and uses the following syntax: {{ expression | filter1 | filter2 | ... }} E.g. the markup {{ 1234 | number:2 }} formats the number 1234 with 2 decimal points using the number filter.
In AngularJS, you can also inject the $filter service within the controller and can use it with the following syntax for the filter. Syntax: $filter("filter")(array, expression, compare, propertyKey) function myCtrl($scope, $filter) { $scope. finalResult = $filter("filter")( $scope.
Adding Filters to Expressions Filters can be added to expressions by using the pipe character | , followed by a filter.
Filters can be added in AngularJS to format data to display on UI without changing the original format. Filters can be added to an expression or directives using the pipe | symbol. AngularJS Filters: AngularJS provides filters to transform data of different data types.
An approach is to use a function to return the filtered data:
function MainCtrl($scope, $filter) {
$scope.numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
$scope.filteredNumbers = function() {
var result = $scope.numbers;
if ($scope.oddListEnabled) {
result = $filter('oddList')(result);
}
if ($scope.luckyListEnabled) {
result = $filter('luckyList')(result);
}
return result;
};
}
And the template:
<ul>
<li ng-repeat="number in filteredNumbers()">
{{number}}
</li>
</ul>
Here is a plunkr: http://plnkr.co/edit/4ectDA?p=preview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With