This code here works perfectly in JSFiddle:
var app = angular.module('app', []);
app.filter('termsFilter', termsFilter);
function termsFilter(){
function parseString(input){
// console.log('input', input);
return input.split(",");
}
function findIndex(valueToSearch, theArray, currentIndex) {
// console.log('currentIndex', currentIndex);
if (typeof(currentIndex) === 'undefined') currentIndex = '';
// console.log('targetArray', theArray);
if(Array.isArray(theArray)) {
for (var i = 0; i < theArray.length; i++) {
// console.log('is theArray[' + i + ']', theArray[i]);
if (theArray[i].title == valueToSearch) {
console.log('found one');
return true;
}
}
return false;
}
}
return function (array, propertyString, target){
if(target && typeof(target) !== 'undefined'){
target = parseString(target);
}
// console.log('target:' ,target);
return _.filter(array, function(item){
if(!target || typeof(target) === 'undefined'){
return true;
}
var targetArray = item.terms[propertyString];
var returnStatus = false;
angular.forEach(target, function(value, key){
// console.log('=======================================');
// console.log('item:' ,item);
// console.log('value:' ,value);
// console.log('---------------------------------------');
if(findIndex(value, targetArray)) {
returnStatus = true;
}
});
// console.log('item:' + item.id ,returnStatus);
return returnStatus;
});
};
}
app.controller("ctrl", function cntrl($scope){
$scope.data = [
{
"id": 194,
"terms": {
"cats": [
{
"id": 6,
"title": "Refrigerated"
}
],
"diet": [
{
"id": 2,
"title": "Non-GMO"
},
{
"id": 1,
"title": "Organic"
}
]
}
},
{
"id": 195,
"terms": {
"cats": [
{
"id": 6,
"title": "Grocery"
}
],
"diet": [
{
"id": 1,
"title": "Vegan"
}
]
},
},
{
"id": 196,
"terms": {
"cats": [
{
"id": 6,
"title": "Grocery"
}
],
"diet": [
{
"id": 2,
"title": "Non-GMO"
},
{
"id": 1,
"title": "Organic"
},
{
"id": 9,
"title": "Vegan"
}
]
},
},
{
"id": 197,
"terms": {
"cats": [
{
"id": 6,
"title": "Fresh"
}
],
"diet": [
{
"id": 1,
"title": "Organic"
}
]
},
}
];
});
Working fiddle here: http://ift.tt/1K5wjlj
But when I try to use it in my application, I get the error:
ReferenceError: _ is not defined
Please tell me: What does the _. refer to, and how can I fix the code to work inside my app, too?
Aucun commentaire:
Enregistrer un commentaire