0

Problem: when i input "K", it filters both name and country that contain character "K".

Question: How can i filter the input characters only in "names.name"?

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

app.controller("namesCtrl", function($scope) {
    $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' }
    ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="namesCtrl">
    <p>
        <input type="text" ng-model="test">
    </p>
    <ul>
        <li ng-repeat="name in names | filter:test">
            {{ name.name }}
        </li>
    </ul>
</div>

2
  • Have a look at this link. Hope it helps. Commented Oct 9, 2018 at 9:07
  • 2
    select a key to search through, e.g. : | filter: {'name' : test} Commented Oct 9, 2018 at 9:07

2 Answers 2

1

Try this

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

app.controller("namesCtrl", function($scope) {
    $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' }
    ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="namesCtrl">
    <p>
        <input type="text" ng-model="test">
    </p>
    <ul>
        <li ng-repeat="name in names | filter:{'name':test}">
            {{ name.name }}
        </li>
    </ul>
</div>

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

Comments

0

Could you please use custom filter like below.

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

app.controller("namesCtrl", function($scope) {
  $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'
    }
  ];
});

app.filter('searchTerm', function() {
  return function(items, text) {
    if (text) {
      return items.filter(item => {
        return item.name.toLowerCase().includes(text)
      })
    }
    return items;
  };
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="namesCtrl">
  <p>
    <input type="text" ng-model="test">
  </p>
  <ul>
    <li ng-repeat="name in names | searchTerm:test">
      {{ name.name }}
    </li>
  </ul>
</div>

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.