angularjsHow do I use the AngularJS filter pipe?
The AngularJS filter pipe is used to filter an array of data to return a subset of the data that meets certain criteria.
Example
<div ng-app="myApp" ng-controller="MyController">
<ul>
<li ng-repeat="person in people | filter:myFilter">
{{ person.name }}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('MyController', function($scope) {
$scope.people = [
{name: 'John', age: 25},
{name: 'Jane', age: 30},
{name: 'Jim', age: 27}
];
$scope.myFilter = function(person) {
return person.age > 25;
};
});
</script>
Output example
- Jane
- Jim
The code above uses the AngularJS filter pipe to filter the array of people and return only the people with an age greater than 25.
The code consists of the following parts:
- A
<div>element with anng-appandng-controllerattribute to define the application and controller. - An
<ul>element with anng-repeatattribute to loop through each item in thepeoplearray. - A
filter:myFilterexpression in theng-repeatattribute to use themyFilterfunction to filter thepeoplearray. - A
$scope.peoplearray of objects with each object containing anameandageproperty. - A
$scope.myFilterfunction to filter thepeoplearray and return only the people with an age greater than 25.
For more information on the AngularJS filter pipe, see the AngularJS documentation.
More of Angularjs
- How can I become an Angular expert from a beginner level?
- How do I use Angular to zip files?
- How can I use Angular to zoom in and out of a div?
- How do I use Angular Zone to run my code?
- How can I use Angular and Zorro together to create a software application?
- How can I use Angular to zoom in on an image?
- How can I prevent XSS attacks when using AngularJS?
- How can I use Zone.js with Angular to detect and act upon asynchronous events?
- How do I use Angular Zone to detect and run Angular change detection?
- How do I use Angular with YAML?
See more codes...