Here is the simple scenario of an Angularjs filter in template:
<input ng-model="query">
<li ng-repeat="item in filtered = (prosItems.results |filter:mySearch)">
Because I need to filter on specific fields only from prosItems.results, i created in the controller a specific function:
$scope.mySearch = function (item){
if ($scope.query) {
var searchTerm = $scope.query.toLocaleLowerCase();
return item.firstName.toLowerCase().indexOf(searchTerm)!==-1 ||
item.lastName.toLowerCase().indexOf(searchTerm)!==-1 ||
item.company.toLowerCase().indexOf(searchTerm)!==-1 ||
item.city.toLowerCase().indexOf(searchTerm)!==-1 ||
item.state.toLowerCase().indexOf(searchTerm)!==-1 ||
item.country.toLowerCase().indexOf(searchTerm)!==-1;
} else {
return true;
}
};
Problem is when any property of an iteration is undefined, for example if user hasn't specified a "company" on an entry, I will get that error as soon as I type something in the input field:
Cannot call method 'toLowerCase' of undefined
I could imagine solution if I had only one property to iterate trough, but since i have several, I don't see practical way of doing so. Do you see any way to solve this issue practically ?