AspiringCanadian
AspiringCanadian

Reputation: 1663

Angular filter search with similar and not exact words

The filter only finds text that matches exactly but not similar. E.g. if one of the entries was "John Dwayne Smith", user has to type those words in exact order for them to be searched by the filter, but how to make the search work in a way that the "Hello there, world!" gets displayed even when a person types a combination of those words, as in, "John Smith"?

HTML:

<input type="text" placeholder="Search" ng-model="userSearch">

   <div ng-repeat="user in users | filter:userSearch">
     <div>
       {{user.firstname}} {{user.lastname}}
     </div>
   </div>

JS:

angular.module('myApp', [])
  .controller('myCtrl', function($scope){

  $scope.users = [

    {firstname: 'john', lastname: 'dwayne smith'},
    {firstname: 'jane', lastname: 'dwayne due'},
    {firstname: 'bob', lastname: 'dwayne rand'}

  ];

});

JSBin Example

Upvotes: 0

Views: 2420

Answers (2)

Elka
Elka

Reputation: 237

When you use angular filter, | filter works as 'like' but if you add :true in the end, then it works as 'equal'

Upvotes: 0

Kyle
Kyle

Reputation: 5557

Have a look at angular-filter. It provides a fuzzy filter.

<input type="text" ng-model="search" placeholder="search book" />
<li ng-repeat="book in books | fuzzy: search">
  {{ book.title }}
</li>
<!--case sensitive-->
<li ng-repeat="book in books | fuzzy: search: true">
  {{ book.title }}
</li>

Upvotes: 1

Related Questions