Let's imagine we have this array
$scope.myArr = [{
name: 'Marc Rasmussen',
phone: 239470192,
title: 'It Dude',
description: 'Hello my name is Marc i am testing the fact that i can search for two fields in an object'
}, {
name: 'Louise',
phone: 1234567890,
title: 'HR Director',
description: 'I am also testing'
}, {
name: 'Leonardo',
phone: 123499,
title: 'Actor',
description: 'I have never won an oscar'
}];
As you can see, we have these fields:
- name
- phone
- title
- description
To display these fields, we use ng-repeat
<div ng-repeat="obj in myArr">
<div>
name: {{obj.name}}
</div>
<div>
phone: {{obj.phone}}
</div>
<div>
title: {{obj.title}}
</div>
<div>
description: {{obj.description}}
</div>
</div>
Now, what we want is a single text field that can search for results in both the name
and description
fields simultaneously.
Usually, you would use a variable like $scope.search.$
, but this searches in all fields. Alternatively, you could create an input field for each field, but that doesn't achieve what we need.
So, the question is, how can we make one input search in two fields at the same time?
Here is a fiddle link to the above code: