【发布时间】:2017-06-02 03:03:50
【问题描述】:
我正在使用 Angular 4 和 Materials 2。
我已经使用数据数组成功创建了一些自动完成字段。这是我的控制器:
sectorCtrl;
allSectors
filteredSectors: any;
constructor() {
this.sectorCtrl = new FormControl();
this.filteredSectors = this.sectorCtrl.valueChanges
.startWith(null)
.map(name => this.filterValues(name));
}
filterValues(val: string) {
return val ? this.allSectors.filter(s => new RegExp(`^${val}`, 'gi').test(s.label)) : this.allSectors;
}
还有我的模板:
<md-input-container>
<input mdInput placeholder="Sectors" [mdAutocomplete]="auto" [formControl]="sectorsCtrl">
</md-input-container>
<md-autocomplete #auto="mdAutocomplete" [displayWith]="displayFn">
<md-option *ngFor="let value of filteredSectors | async" [value]="value" >
{{ value.label }}
</md-option>
</md-autocomplete>
如何调整代码以使用远程 API?
【问题讨论】:
标签: api angular autocomplete angular-material2