Skip to content

Instantly share code, notes, and snippets.

@Agnostic
Last active December 21, 2015 00:58
Show Gist options
  • Save Agnostic/6223945 to your computer and use it in GitHub Desktop.
Save Agnostic/6223945 to your computer and use it in GitHub Desktop.
Custom filter for ngRepeat using objects (AngularJS)
<div ng-controller='listController'>
<input type='text' ng-model='filterInput'>
<ul>
<li ng-repeat='item in items | filter:customFilter'>
{{ item.name }}
</li>
</ul>
</div>
app.controller('listController', ['$scope', function($scope){
// Sample data
$scope.items = [
{
id: 1,
name: 'Item 1'
},
{
id: 2,
name: 'Item 2'
},
{
id: 3,
name: 'Item 3'
}
];
// Our custom filter
$scope.customFilter = function(){
if(!$scope.filterInput) return true;
var regExp = new RegExp($scope.filterInput, 'gi');
return regExp.test(item.name);
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment