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-app
andng-controller
attribute to define the application and controller. - An
<ul>
element with anng-repeat
attribute to loop through each item in thepeople
array. - A
filter:myFilter
expression in theng-repeat
attribute to use themyFilter
function to filter thepeople
array. - A
$scope.people
array of objects with each object containing aname
andage
property. - A
$scope.myFilter
function to filter thepeople
array 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 create an editable AngularJS application?
- How do I use Angular to zip files?
- How can I use AngularJS to create a zone in my software development project?
- How can I use Angular to zoom in and out of a div?
- How can I prevent XSS attacks when using AngularJS?
- How do I use Angular with YAML?
- How do I use the AngularJS router?
- How do I use the window.open function with AngularJS?
- How do I create a yes/no dialog box using Angular?
- How do I reload a component in AngularJS?
See more codes...