Custom comparator for only one property in Angular $filter
First, my reason for creating the custom comparator in the first place is
because an attribute on the expression that is an array--such as
'tags'--required the expression to have the values in a particular order
and wouldn't match otherwise. (So an expression with tags['a', 'b'] would
match ['x', 'a', 'b', 'y'] but not ['x', 'b', 'a', 'y'].)
I've been able to get around this because in Angular's $filter service, it
allows you to provide a custom comparator. However, I'm only able to
filter based on the attributes type, not the key's value.
I want a custom comparator for only one of the attributes in an object. Is
there a way to do that without copying and modifying Angular's source
code?
Here is an example of my custom comparator: (found in
http://plnkr.co/edit/eFeinQhHTIZrzXBm4srq )
$scope.inclusiveFilter = function(expected, actual){
if(expected && actual){
console.log(expected, actual);
if(angular.isArray(expected) && angular.isArray(actual)){
for(var t in actual){
if(expected.indexOf(actual[t]) == -1){
return false;
}
}
}
return true;
}
}
In my example the filter on the tags works just fine, but it no longer
works on $ to match any property, I am only able to compare attributes
based on their type, not their name. So an expression like this could
never work:
{
includeThese: ['a', 'b'],
excludeThese: ['x', 'y']
}
No comments:
Post a Comment