Tob*_*obi 5 autocomplete typescript angular-material2 angular
我遇到的问题如下:我需要的行为是,当从自动完成输入中选择一个选项时,它应该向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>
Run Code Online (Sandbox Code Playgroud)
这是我的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)
}
}
Run Code Online (Sandbox Code Playgroud)
我已经检查了Angular Material文档,但我还没有找到一种方法来执行此操作.
谢谢.
我认为你很接近,看起来唯一缺少的部分是重置表单控件值selectCategory.这就是我们在自己的应用程序中完成它的方式,但它实际上是相同的:
/** Reference to the autocomplete trigger. */
@ViewChild(MdAutocompleteTrigger)
private trigger: MdAutocompleteTrigger;
/** Form control for the input. */
searchControl = new FormControl('');
ngAfterViewInit() {
// Clear the input and emit when a selection is made
this.trigger.autocomplete.optionSelected
.map(event => event.option)
.subscribe(option => {
// This may or may not be needed, depending on your purposes
option.deselect();
// Emit the selection (so parent component can add chip)
this.selection.emit(option.value);
// Reset the value of the input
this.searchControl.setValue('');
});
}
Run Code Online (Sandbox Code Playgroud)
每当您选择一个值时,所选文本都会有一个简短的"闪烁".为避免这种情况,您可以使用该displayWith属性将选定值显示为空:
<md-autocomplete #auto="mdAutocomplete" [displayWith]="displayNull">
...
</md-autocomplete>
/** Display function for selected autocomplete values. */
displayNull(value) {
return null;
}
Run Code Online (Sandbox Code Playgroud)
这是我的方法
在模板中
...
<md-autocomplete #auto="mdAutocomplete" [displayWith]="displayNull" (optionSelected)="selectHandler($event)">
...
Run Code Online (Sandbox Code Playgroud)
在组件.ts中
public selectHandler(event : MdAutocompleteSelectedEvent) : void
{
event.option.deselect()
this.doStuffWith(event.option.value)
}
public displayNull()
{
return null
}
Run Code Online (Sandbox Code Playgroud)
遇到了这个解决方案,但我不喜欢这个displayNull修复。
我的解决方案看起来与此类似:
组件.html:
<input matInput [matAutocomplete]="auto" (input)="filter($event.target.value)" #autoInput>
<mat-autocomplete #auto [displayWith]="displayKey" (optionSelected)="emit($event, autoInput)">
// ...
</mat-autocomplete>
Run Code Online (Sandbox Code Playgroud)
组件.ts:
@Output() optionSelected = new EventEmitter<MatAutocompleteSelectedEvent>();
emit(event: MatAutocompleteSelectedEvent, ele: HTMLInputElement) {
ele.value = '';
ele.blur();
this.filter('');
this.optionSelected.emit(event);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13131 次 |
| 最近记录: |