As a beginner in the world of computers, I am attempting to create a customized filter for an AngularJS application.
My objective is to search through a string of integers (cultivos.age) and retrieve the item if the string meets the mes.age condition. I have successfully developed a working filter that returns == values. However, I am struggling with implementing an 'indexOf' condition that would return the item if mes.age is present within the cultivos.age array.
The functional filter:
angular.module('calendarioFilters',[]).filter('filtroMes', function(){ return function(cultivos, name){ var arrayToReturn = []; for (var i=0; i<cultivos.length; i++){ if (cultivos[i].age == this.mes.age) { arrayToReturn.push(cultivos[i]); } } return arrayToReturn; };
});
and the non-functional filter:
angular.module('calendarioFilters',[]).filter('filtroMes', function(){ return function(cultivos, name){ var arrayToReturn = []; for (var i=0; i<cultivos.length; i++){ if (cultivos[i].age.indexOf(this.mes.age) !== -1) { arrayToReturn.push(cultivos[i]); } } return arrayToReturn; };
});
I am using the filter in HTML:
<li ng-repeat="cultivo in cultivos | filtroMes:''">
Even the functioning filter logs "TypeError: Cannot read property 'length' of undefined."
Thank you!