我的代码 //从webservice获取所有国家/地区
this.ws.AllCountry().subscribe(
countryes => {
this.countryes = countryes.map((country) => {
return new Country(country);
});
}
);
//创建FilterOptions
this.stateCtrl = new FormControl();
this.filteredOptionsCountry = this.stateCtrl.valueChanges
.pipe(
startWith(''),
map(country => country ? this.filterCountry(country) : this.countryes.slice()) // Cannot read property 'slice' of undefined
);
//过滤国家/地区
filterCountry(name: string) {
return this.countryes.filter(country =>
country.name.toLowerCase().indexOf(name.toLowerCase()) === 0);
}
Code.html
<form class="example-form">
<mat-form-field class="example-full-width">
<input matInput placeholder="Select Country" aria-label="State" [matAutocomplete]="auto" [formControl]="stateCtrl">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let country of filteredOptionsCountry | async" [value]="country.country_id">
{{ country.name }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
你能告诉我一些想法,问题是什么?
谢谢
答案 0 :(得分:0)
我认为你在订阅内部之前使用的是乡村人,我建议在allCountry订阅中移动你的“Create FilterOptions”代码,其中定义了countryes
答案 1 :(得分:0)
尝试一下...
map((country:string | null)=> country?this._filter(country):this.countryes.slice()));