您制作的 DOM 是正确的。
<md-autocomplete md-selected-item="selectedItem" md-search-text="searchText" md-items="item in getMatches(searchText)" md-item-text="item.display">
<md-item-template>
<span md-highlight-text="searchText">{{item.display}}</span>
</md-item-template>
<md-not-found>
No matches found.
</md-not-found>
</md-autocomplete>
但是下面显示的函数是错误的,因为它没有返回任何内容,这就是您得到 “未找到匹配项”的原因。
app.controller('IndexController', function ($scope) {
$scope.getMatches = function (text) {
alert(text);//this does not return anything
}
});
现在下一个问题应该返回什么。
它应该返回一个如下所示的 JSON 数组。
[{
value: "apple",
display: "apple"
}, {
value: "banana",
display: "banana"
}, {
value: "gauva",
display: "gauva"
}, {
value: "melon",
display: "melon"
}, {
value: "potato",
display: "potato"
}, {
value: "carrot",
display: "carrot"
}, {
value: "cauliflower",
display: "cauliflower"
}, {
value: "jasmine",
display: "jasmine"
}, {
value: "cabbage",
display: "cabbage"
}, {
value: "peas",
display: "peas"
}]
display key
JSON 中的内容是什么
由于您在此处提到,md-item-text="item.display"
因此返回的数组必须具有显示在自动完成下拉菜单中的显示键。
所以我的搜索功能是这样的:
$scope.myDta = [{
value: "apple",
display: "apple"
}, {
value: "banana",
display: "banana"
}, {
value: "gauva",
display: "gauva"
}, {
value: "melon",
display: "melon"
}, {
value: "potato",
display: "potato"
}, {
value: "carrot",
display: "carrot"
}, {
value: "cauliflower",
display: "cauliflower"
}, {
value: "jasmine",
display: "jasmine"
}, {
value: "cabbage",
display: "cabbage"
}, {
value: "peas",
display: "peas"
}];
$scope.getMatches = function (text) {
text = text.toLowerCase();
var ret = $scope.myDta.filter(function (d) {
//return element which starts with entered text
return d.display.startsWith(text);
});
return ret;
}
工作代码在这里
测试用例:类型ca
希望这可以帮助!