我嘗試了在論壇找之前的文章去參考,但是都不明白,怎樣去解決。
我試試matAutoComplete material,它出現以下ERROR。
ERROR TypeError: countries.filter is not a function
at MapSubscriber.project (app.component.ts:68)
at MapSubscriber._next (map.js:29)
at MapSubscriber.next (Subscriber.js:49)
at MapSubscriber._next (map.js:35)
at MapSubscriber.next (Subscriber.js:49)
at FilterSubscriber._next (filter.js:33)
at FilterSubscriber.next (Subscriber.js:49)
at MergeMapSubscriber.notifyNext (mergeMap.js:72)
at InnerSubscriber._next (InnerSubscriber.js:11)
at InnerSubscriber.next (Subscriber.js:49)
我的app.component.html
<input type="text" name="country" matInput placeholder="國家" formControlName="country" [matAutocomplete]="countries" />
<mat-autocomplete #countries=“matAutocomplete”>
<mat-option *ngFor=“let country of countries$ | async” [value]=“country.name” >
{{ country.name }}
我的app.component.ts
this.surveyForm
.get('country')
.valueChanges.pipe(debounceTime(300))
.subscribe(inputCountry =>
{
console.log(inputCountry);
this.countries$ = this.httpClient.get<any[]>('assets/countries.json', options).pipe(map(countries => {
console.log('countries:'+countries);
return countries.filter(country => country.name.indexOf(inputCountry) >= 0);
}));
});
console.log都是東西顯示出來。
countries.filter(country => country.name.indexOf(inputCountry) >= 0); 是不是這個有問題。
我是參考以下網頁去做的
https://ithelp.ithome.com.tw/articles/10194495
Thank you very much.