【发布时间】:2018-02-26 08:08:49
【问题描述】:
我遇到的问题如下:我需要的行为是,当从自动完成输入中选择一个选项时,它应该向 Angular Material Chips 组件添加一个芯片(它确实如此)并且它也应该清除自动完成输入,所以我可以选择另一个选项。
这是我的html:
<div class="col-md-6">
<md-form-field>
<input type="text" placeholder="Categoría" aria-label="Categoría" mdInput [mdAutocomplete]="auto" [formControl]="categoryCtrl">
<md-autocomplete #auto="mdAutocomplete" (optionSelected)="selectCategory($event)">
<md-option *ngFor="let category of filteredCategories | async" [value]="category.name">
{{ category.name }}
</md-option>
</md-autocomplete>
</md-form-field>
</div>
这是我的 TypeScript 代码:
constructor(private placeService: PlaceService, private categoryService: CategoryService) {
this.categoryCtrl = new FormControl();
this.filteredCategories = this.categoryCtrl.valueChanges
.startWith('')
.map(category => category ? this.filterCategories(category) : this.categories.slice());
}
filterCategories(name: string) {
return this.categories.filter(category => category.name.toLowerCase().indexOf(name.toLowerCase()) === 0);
}
selectCategory(category: any) {
const index = this.selectedCategories.indexOf(category.option.value);
if (index === -1) {
this.selectedCategories.push(category.option.value)
}
}
我查看了 Angular Material 文档,但没有找到执行此操作的方法。
谢谢。
【问题讨论】:
标签: angular typescript autocomplete angular-material2