Weijing Jay Lin
Weijing Jay Lin

Reputation: 3238

How do I apply a filter to a specific property in AngularJS?

I found angular filter has filter: value but it would search whole string, for instance here is the data

$scope.names = [
    {name:'Jani',country:'Norway'},
    {name:'Carl',country:'Sweden'},
    {name:'Margareth',country:'England'},
    {name:'Hege',country:'Norway'},
    {name:'Joe',country:'Denmark'},
    {name:'Gustav',country:'Sweden'},
    {name:'Birgit',country:'Denmark'},
    {name:'Mary',country:'England'},
    {name:'Kai',country:'Norway'}
  ];

I want to only display the names that have the letter 'e' in them. But it's also matching the countries with 'e' in the name.

How to I match to only a specific property in a filter?

Upvotes: 1

Views: 1206

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

You could do filter on single property, by having providing JSON object to filter.

Markup

Name
<input ng-model="search" type="text"/>

<ul>
  <li ng-repeat="item in names | filter: {name: search }">{{item.name}}<li>
</ul>

Upvotes: 4

Related Questions