Angular JS Dynamic searching for a collection of objects, with min & max range (i.e., persons with age from 18 to 60)

View:-

<div ng-repeat=”car in cars | filter: byRange(fieldname, min_value, max_value” >

…….

……

</div>

Controller:-

$scope.byRange = function (fieldName, minValue, maxValue) {
if (minValue === undefined) minValue = Number.MIN_VALUE;
if (maxValue === undefined) maxValue = Number.MAX_VALUE;

return function predicateFunc(item) {
return minValue <= item[fieldName] && item[fieldName] <= maxValue;
};
};

That’s it just pass in the field name, min_value and max_value to the ‘byRange’ function and get the dynamic searching for collection of objects.

Advertisements