Custom Filter App
HTML
<div ng-app="myApp">
<h1>
Creating Custom Filter
</h1>
<div ng-controller="myCtrl">
<input type="text" ng-model="inputAge"/>
<ul>
<li ng-repeat="p in people | filterAdults:inputAge">
{{ p.name }} ({{p.age}})
</li>
</ul>
</div>
</div>
JavaScript
var app = angular.module("myApp", []);
app.controller('myCtrl', function ($scope) {
$scope.sampleText = "my custom filter";
$scope.people = [
{ name:"John", age: 23 },
{ name:"Linda", age: 19},
{ name:"Craig", age: 17 },
{ name:"Simon", age: 21},
{ name:"Mary", age: 15 }
];
});
app.filter('filterAdults', function () {
return function (items, iAge) {
var adults = [];
if(iAge) {
angular.forEach(items, function(item) {
if(item.age > iAge) {
this.push(item)
}
}, adults);
return adults;
} else {
return items;
}
};
});