0

用于标记(md-chips)的 md-autocomplete 的 Angular 材料文档使用下面给出的过滤器功能 -

    //Search for vegetables.
    function querySearch (query) {
      var results = query ? self.vegetables.filter(createFilterFor(query)) : [];
      return results;
    }

    //Create filter function for a query string
     function createFilterFor(query) {
      var lowercaseQuery = angular.lowercase(query);
       return function filterFn(vegetable) {
          return (vegetable._lowername.indexOf(lowercaseQuery) === 0) ||
            (vegetable._lowertype.indexOf(lowercaseQuery) === 0);
       };
     }

来源:https ://material.angularjs.org/latest/demo/chips

有人可以解释这两个功能吗?在 querySearch 方法中,“self.vegetables.filter(createFilterFor(query)) : [];”是什么意思?意思是?

(“vegetables”是所有对象的数组,其中将给出建议,查询是搜索文本)

4

1 回答 1

0
function querySearch (query) {
      var results = query ? self.vegetables.filter(createFilterFor(query)) : [];
      return results;
    }

如果查询不为空,则返回蔬菜列表,但如果查询为空,则返回 void

//Create filter function for a query string
     function createFilterFor(query) {
      var lowercaseQuery = angular.lowercase(query);
       return function filterFn(vegetable) {
          return (vegetable._lowername.indexOf(lowercaseQuery) === 0) ||
            (vegetable._lowertype.indexOf(lowercaseQuery) === 0);
       };
     }

该函数首先将查询字符串小写并检查查询是否在列表中

于 2016-07-22T05:43:14.327 回答