あなたが作っている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
お役に立てれば!