Last active
December 21, 2015 00:58
-
-
Save Agnostic/6223945 to your computer and use it in GitHub Desktop.
Custom filter for ngRepeat using objects (AngularJS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div ng-controller='listController'> | |
<input type='text' ng-model='filterInput'> | |
<ul> | |
<li ng-repeat='item in items | filter:customFilter'> | |
{{ item.name }} | |
</li> | |
</ul> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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