Created
September 18, 2015 19:30
-
-
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.
This file contains 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
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