标签的md-autocomplete(md-chips)的角度材料文档使用下面给出的过滤函数 -
//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"是所有对象的数组,其中将给出建议,查询是搜索文本)
答案 0 :(得分:0)
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);
};
}
该函数首先将查询字符串小写并检查查询是否在列表中