角度过滤器:"返回_.filter"适用于JSFiddle,但不适用于应用程序

时间:2015-01-28 19:34:25

标签: javascript angularjs

此代码在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"
          }
        ]
      },
    }

];

});

在这里工作小提琴:http://jsfiddle.net/earthman/ho16td1o/

但是当我尝试在我的应用程序中使用它时,我收到错误:

ReferenceError: _ is not defined

请告诉我:_是什么。请参阅,以及如何修复代码以在我的应用程序内部工作?

2 个答案:

答案 0 :(得分:0)

http://jsfiddle.net/earthman/ho16td1o/

您的小提琴导入underscore.js

您的项目也应如此。

当您查看jsFiddle时,左侧是一个引用它的外部资源部分。

答案 1 :(得分:0)

ReferenceError: _ is not defined 

_用于调用underscore.js函数。

fiddle中,您在外部资源中拥有underscore.js

因此,您需要在自己的本地项目中导入它。

您可以从这里下载http://underscorejs.org/