Skip to content

Instantly share code, notes, and snippets.

@calebdwilliams
Created September 18, 2015 19:30
Show Gist options
  • Save calebdwilliams/46fcb67a426ad717c3e0 to your computer and use it in GitHub Desktop.
Save calebdwilliams/46fcb67a426ad717c3e0 to your computer and use it in GitHub Desktop.
Defines a filter on the $filter provider that will call a method to filter objects inside of ngRepeat.
angular.module('myApp.filters', [])
/**
* Defines a filter on the $filter provider that will call a method
* to filter objects inside of ngRepeat.
*
* @return {Function}
* @param {Array} - the array to loop through
* @param {String} - the Object method name
* @param {Any} - the value to match against
* @return {Array} - the resultant array
*/
.filter('evalMethod', [function() {
return function(array, method, expected) {
var results = [];
/**
* loop through all items in the array
* and bind the results to the results object
*/
angular.forEach(array, function(item, key) {
/**
* call the method against the item context,
* if it returns as expected, push it into the results array
*/
if (item[method].call(item) == expected) {
this.push(item);
}
}, results);
return results;
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment