Friday, July 8, 2016

AngularJS Filter Exact match / Filter Strict search

I wanted to get results with exact match using filter property in AngularJS.

Let us assume I have JSON values;

$scope.languages = [
        {id:19, name:"German", available: false},    
        {id:119, name:"English", available: true},    
        {id:3, name:"French", available: false},  
        {id:4, name:"Italian", available: true},  
        {id:5, name:"Spanish", available: false}
    ];

I wanted to display only German using id. Then my AngularJS filter would be;

<li ng-repeat="language in languages | filter: {id: 19}">{{language.name}}</li>

Unfortunately, I got two results; German and English
I was confused and found the reason that it is searching the IDs with "contains" keyword.
German's ID is 19
English's ID is 119

Therefore the filter value 19 is present in both German and English.

How do I get results with exact match?
Answer: Mention :true in filter

<li ng-repeat="language in languages | filter: {id: 19}:true">{{language.name}}</li>


Here is the working example in jsfiddle;

Issue: http://jsfiddle.net/to7z06ma/65/

Fix: http://jsfiddle.net/to7z06ma/66/


0 comments:

Post a Comment