2

Says we have a search input which filters data by two properties: name and id

<input ng-model="query.name">
<input ng-model="query.id">

<div ng-repeat="item in items | filter:query"></div>

How can I do this in one simple input ng-model

Thanks in advance

1
  • 1
    you can write a custom filter Commented Oct 30, 2014 at 4:13

3 Answers 3

1

Really simple, you are almost there:

<input ng-model="query">
<div ng-repeat="item in items | filter:query">{{item}}</div>

This way you have only one ng-model to filter.

--
Edit:

If you have more properties in your object that don't want to be filtered, you can use a function or a custom filter, from the documentation:

{{ filter_expression | filter : expression : comparator}}
Expression can be: string, Object or function()

I've updated the fiddle to demonstrate how you can filter using both a function and a custom filter: fiddle.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for writing the example, but what if there is other useless properties which I don't want to included, I slightly changed the example: jsfiddle.net/k9s4Lvs4/1
@klamtlne, I've edited my answer to add a new fiddle for this issue.
1

Haven't tried it, but it looks like from the docs at https://docs.angularjs.org/api/ng/filter/filter that you can do something like this:

<input ng-model="query.$">

See the last example on the page and the part that talks about the "Object" version of the "expression" argument to the filter.

1 Comment

This's a gr8 resolution for this example, but real-life, there are some useless properties which will also be included this way.
1

You can easily solve this problem with angular-filter.

https://github.com/a8m/angular-filter

CONTROLLER

$scope.users = [
  { first_name: 'Sharon', last_name: 'Melendez' },
  { first_name: 'Edmundo', last_name: 'Hepler' },
  { first_name: 'Marsha', last_name: 'Letourneau' }
];

HTML

<input ng-model="search" placeholder="search by full name"/>
<th ng-repeat="user in users | searchField: 'first_name': 'last_name' | filter: search">
  {{ user.first_name }} {{ user.last_name }}
</th>
<!-- so now you can search by full name -->

As simple as that.

Good Luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.