0

I created a page using AngularJS.

$scope.search = function (item) {
    if ($scope.searchText == undefined) {
        return true;
    }
    else {
        if (item.city.toLowerCase().indexOf($scope.searchText.toLowerCase()) != -1 || item.name.toLowerCase().indexOf($scope.searchText.toLowerCase()) != -1)
        {
            return true;
        }
    }
    return false;
}

Link : http://plnkr.co/edit/sDtDyKDw2pPJC5Ww05W4?p=preview

The program runs fine. I set a debugger in the console and checked the function and found that the search function in the script is called twice for each (ng-repeat)item. i.e. 10 times in total. What is the reason that it goes around again?

TagHeuer
  • 29
  • 8

2 Answers2

0

Change your filter value as searchText

<tr ng-repeat="employee in employees | filter: searchText" >
   <td>{{employee.name}}</td>
   <td>{{employee.gender}}</td>
   <td>{{employee.salary}}</td>
   <td>{{employee.city}}</td>
</tr>

<tr ng-repeat="employee in employees | filter:{city:searchText}" >
byteC0de
  • 5,153
  • 5
  • 33
  • 66
0

Not better with this ? :)

<tr ng-repeat="employee in employees | filter: searchText" >

Or if you want to search only in 'city' column

 <tr ng-repeat="employee in employees | filter:{city:searchText}" >

Hope it helps.

Sparw
  • 2,688
  • 1
  • 15
  • 34