Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

$filter does not work with empty string #7890

Closed
tkrotoff opened this issue Jun 18, 2014 · 7 comments
Closed

$filter does not work with empty string #7890

tkrotoff opened this issue Jun 18, 2014 · 7 comments

Comments

@tkrotoff
Copy link

$scope.persons = [
  {
    firstName: 'Nicolas',
    lastName: 'John'
  },
  {
    firstName: 'Julien',
    lastName: 'Smith'
  },
  {
    firstName: 'Thomas',
    lastName: ''
  }
];

// WORKS, returns item {firstName:'Thomas', lastName:''}
$scope.thomasFilter = $filter('filter')($scope.persons, {firstName: 'Thomas'});

// FAILS, returns the entire array instead of {firstName:'Thomas', lastName:''}
$scope.emptyStringFilter = $filter('filter')($scope.persons, {lastName: ''});

See demo: http://plnkr.co/edit/D9zi5h?p=preview

Tested with AngularJS 1.2.18 and 1.3.0-beta.13

@caitp
Copy link
Contributor

caitp commented Jun 18, 2014

This works as documented --- if you want to do an exact string match with the filterFilter, you should use a predicate function which performs the comparison for you. Obviously, any string will "contain the empty string".

You can also write a custom filter which performs exact string searches

@rolfyone
Copy link
Contributor

I finally got the answer and was beaten :)
further to what caitp said, pass true as the third parameter and it does an implicit angular.equals
$scope.emptyStringFilter = $filter('filter')($scope.persons, {lastName: ''}, true);

ref: https://docs.angularjs.org/api/ng/filter/filter

@tkrotoff
Copy link
Author

Sorry for the bad report, I feel stupid right now...

@rolfyone
Copy link
Contributor

It took me a lot of digging to validate and find out why, i wouldnt feel stupid!
The tests for filter are 'ok', thats where i found tests that showed this behavior, but the manual when you click 'filter' on the ng site doesnt seem to load the above link. if it did, i think you'd have had an easier time of seeing the issue...

the first 'filter' in the menu isn't the actual filter/filter manual page, its just a vague service reference, which didn't help me with the issue at all!

@MrMikeRodriguez
Copy link

This works as documented --- if you want to do an exact string match with the filterFilter, you should use a predicate function which performs the comparison for you. Obviously, any string will "contain the empty string".

You can also write a custom filter which performs exact string searches

I was confused by this too. The documentation doesn't say "contains" is what tricked me.

It says

string: The string is used for matching against the contents of the array. All strings or objects with string properties in array that match this string will be returned. This also applies to nested object properties. The predicate can be negated by prefixing the string with !.

I bolded the key phrase of the paragraph. I didn't know that "match" was the same as "contains".

@gkalpak
Copy link
Member

gkalpak commented Jan 24, 2017

I bolded the key phrase of the paragraph. I didn't know that "match" was the same as "contains".

It is not the same. The thing is that filter can do a lot more than the default behavior (which is what you are referring to), so we need to be a little more generic. As explained in the comparator argument, it is the comparator that determines what "match" means:

Comparator which is used in determining if the expected value (from the filter expression) and actual value (from the object in the array) should be considered a match.

Can be one of:
[...]

  • false: A short hand for a function which will look for a substring match in a case insensitive way.

[...]
Defaults to false.

If someone can come up with a clearer wording, PRs are always welcome 😃

@MrMikeRodriguez
Copy link

@gkalpak
I didn't read that section carefully enough. I think you are right that it does explain this in enough detail. Thanks for the clarification and sorry to not see it at first!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants