NgRepeat: filter does not work when using track at $ index
This is my html
<body>
<div ng-app="repeatdemo" ng-controller="rd">
<input type="text" ng-model="nameSearch" />
<div ng-repeat="x in names track by $index | filter : nameSearch">
{{x }}
</div>
</div>
This is my script
<script>
var rdapp=angular.module("repeatdemo",[]);
rdapp.controller("rd",function($scope){
$scope.names=['deepu','vicky','hello','hey','vickys'];
});
</script>
When I use track by option $index in repeat
, the filter does not work Please check this script and try to delete track by index
inng-repeat
+3
source to share
2 answers
The problem is that you are applying filter and track in
http://jsfiddle.net/n3xnzvvs/1/
<div ng-repeat="x in names | filter : nameSearch track by $index">
+7
source to share
If you check the documentation for ngRepeat , you will find it in the description of the ngRepeat parameter:
For example: element in elements | filter: searchText track by item.id is a template that can be used to apply a filter to items in combination with a tracking expression.
Your code for the ng-repeat directive should look like this:
<div ng-repeat="x in names | filter: nameSearch track by $index">
+4
source to share