Reputation: 1032
I have 2 list. One bigger and one smaller. I have added infinite scroll to keep adding items from bigger list to smaller one.
// Bigger list (data from DB)
var data;
// Smaller list
$scope.projects = [];
$scope.$watch('projectSearch', function (val) {
if (typeof val !== 'undefined' && val !== "") {
//console.log("SEARCH: " + val);
for (var a = 0; a < data.length; a++) {
if (data[a].name.toLowerCase().indexOf(val) > -1) {
console.log("FOUND: " + data[a].name);
if($scope.projects.indexOf(data[a].name) === -1) {
console.log("PUSHED " + data[a].name);
$scope.projects.push( data[a]);
} else {
console.log("ALREADY IN " + data[a].name);
}
}
}
}
});
Search form:
<input id="search" ng-model="projectSearch" placeholder="Search ..."/>
The problem is it keeps pushing items to new list. It does not check this if statement correctly
if($scope.projects.indexOf(data[a].name) === -1) {
console.log("PUSHED " + data[a].name);
$scope.projects.push( data[a]);
}
Upvotes: 1
Views: 72
Reputation: 6381
you should check for data[a]
, not its property name
if($scope.projects.indexOf(data[a]) === -1) {
console.log("PUSHED " + data[a].name);
$scope.projects.push( data[a]);
}
also, you could remove data[a]
after copy to make next search faster
Upvotes: 3
Reputation: 7650
$scope.projects
should be type of string
assuming data[a].name
is string then your indexOf function properly
Upvotes: 1