1

In the code below, when I'm using filter:filterText all the 3 parameters are getting filtered. If I want any of the two parameters to be filtered what expression can I use there?

<div ng-controller="someController">
    <input type="text" ng-model="filterText"></input>
    <ul>
        <li ng-repeat="friend in friends|filter:filterText">
            ({{friend.name}},{{friend.phone}},{{friend.mail}})
        </li>
    </ul>
</div>
<script type="text/javascript">
    var myapp1=angular.module("myApp",[]);
    myapp1.controller("someController",function($scope,$filter){
        $scope.friends=[
            {
                name:"asdf",
                phone:"123456",
                mail:"[email protected]"
            },
            {
                name:"qwe",
                phone:"456887",
                mail:"[email protected]"
            },
            {
                name:"cvb",
                phone:"7786887",
                mail:"[email protected]"
            }
        ];
    });
</script>

2 Answers 2

1

By defining an object for the filter, you can define which fields you want to filter. The following example only searches in the fields name and mail:

var myapp1=angular.module("myApp",[]);

myapp1.controller("someController",function($scope,$filter){
  $scope.friends=[{
    name:"asdf",
    phone:"123456",
    mail:"[email protected]"
  },
  {
    name:"qwe",
    phone:"456887",
    mail:"[email protected]"
  },
  {
    name:"cvb",
    phone:"7786887",
    mail:"[email protected]"
  }];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="someController">
    Name: <input type="text" ng-model="filterName" />
    Email: <input type="text" ng-model="filterEmail" />
    <ul>
        <li ng-repeat="friend in friends|filter:{ name: filterName, mail: filterEmail}">
        ({{friend.name}},{{friend.phone}},{{friend.mail}})
        </li>
    </ul>
</div>

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

2 Comments

that version returns element with the same name and mail address
thats true, the old version checked if the defined strings is contained in the name and email. I have updated my snipped, so now a string for name and a string for email can be provided
0
You can do this : 

<input type="text" ng-model="filterName" placeholder="by name"/>
<input type="text" ng-model="filterMail" placeholder="by mail"/>
<ul>
    <li ng-repeat="friend in friends|filter:filterName|| filterMail">
        ({{friend.name}},{{friend.phone}},{{friend.mail}})
    </li>
</ul>

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.