【发布时间】:2018-07-18 22:26:48
【问题描述】:
你好吗? 我的项目中有一个未定义的问题“切片”。我使用自动补全材料https://material.angular.io/components/autocomplete/examples
我的代码.ts //从webservice获取所有国家
this.ws.AllCountry().subscribe(
countryes => {
this.countryes = countryes.map((country) => {
return new Country(country);
});
}
);
// 创建过滤器选项
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);
}
代码.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>
你能给我一些建议吗,有什么问题?
谢谢
【问题讨论】:
-
消息告诉错误在哪里......
countryes显然是undefined,你需要调试你的代码;) -
如果你能重现这就像 stackblitz 之类的东西会很容易
-
slice() 等于 slice(0)
标签: html angular typescript autocomplete angular-material