Custom filter
by johnoscott
HTML
<div ng-app="">
<div ng-init="friends = [
{name:'John', price:12, phone:'555-1276'},
{name:'Mary', price:12.5, phone:'800-BIG-MARY'},
{name:'Mike', price:12.90, phone:'555-4321'},
{name:'Adam', price:19.9, phone:'555-5678'},
{name:'Julie', price:15.5, phone:'555-8765'}
]">
</div>
Search: <input ng-model="searchText">
<table id="searchTextResults">
<tr><th>Name</th><th>Phone</th><tr>
<tr ng-repeat="friend in friends ">
<td>{{friend.name}}</td>
<td>{{friend.price | dollar }}</td>
<td>{{friend.phone}}</td>
<tr>
</table>
<hr>
</div>
CSS
.ng-invalid { border: 1px solid red; }
JavaScript
// v1.0.1 add custom filter
angular.module('filters', [])
// sample use {{ value | filtername:"param2":"param2" }}
.filter('dollar', function() {
return function(input) {
var out = '';
if (input % 1) { // modulus 1 of a whole number is zero
out = input.toFixed(2);
} else {
out = input.toString();
}
return out;
}
});