[英]Angular4 Material chipset+autocomplete
我正在使用angular 4的芯片組和自動完成功能來開發stackoverflow中的標記系統等功能。 這是我寫的代碼。 沒用
<mat-form-field class="col-md-4 col-md-offset-2">
<mat-chip-list #chipList>
<mat-chip *ngFor="let item of displayItems" [selectable]="selectable"
[removable]="removable" (remove)="remove(item)">
{{item}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input placeholder="Enter Items..."
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add($event)" matInput [matAutocomplete]="auto"/>
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of options" [value]="option">
{{ option }}
</mat-option>
</mat-autocomplete>
</mat-chip-list>
<mat-hint align="end">Press comma or enter after each selection</mat-hint>
</mat-form-field>
以下是TS文件:選項基本上是從中選擇自動完成的。
snacksType: String[];
visible = true;
selectable = true;
removable = true;
addOnBlur = true;
options=['banana','apple','jackfruit','mango', 'grapes', 'kiwi'];
// Enter, comma
separatorKeysCodes = [ENTER, COMMA];
displayItems = [];
add(event: MatChipInputEvent): void {
let input = event.input;
let value = event.value;
// Add our item
if ((value || '').trim()) {
this.displayItems.push(value.trim());
}
// Reset the input value
if (input) {
input.value = '';
}
}
remove(item: any): void {
let index = this.displayItems.indexOf(item);
if (index >= 0) {
this.displayItems.splice(index, 1);
}
}
您可以在“ mat-autocomplete”中使用@Output(optionSelected)並推送新項目。
snacksType: String[];
visible = true;
selectable = true;
removable = true;
addOnBlur = true;
options=['banana','apple','jackfruit','mango', 'grapes', 'kiwi'];
// Enter, comma
separatorKeysCodes = [ENTER, COMMA];
displayItems = [];
add(event: MatChipInputEvent): void {
let input = event.input;
let value = event.value;
// Add our item
if ((value || '').trim()) {
this.displayItems.push(value.trim());
}
// Reset the input value
if (input) {
input.value = '';
}
}
remove(item: any): void {
let index = this.displayItems.indexOf(item);
if (index >= 0) {
this.displayItems.splice(index, 1);
}
}
public addSelect(event) {
let option = event.option;
let value = option.value;
if ((value || '').trim()) {
this.fruits.push({ name: value.trim() });
}
}
在您的墊自動完成中添加(optionSelected)=“ addSelect($ event)”
<mat-form-field class="col-md-4 col-md-offset-2">
<mat-chip-list #chipList>
<mat-chip *ngFor="let item of displayItems" [selectable]="selectable"
[removable]="removable" (remove)="remove(item)">
{{item}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input placeholder="Enter Items..."
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add($event)" matInput [matAutocomplete]="auto"/>
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="addSelect($event)">
<mat-option *ngFor="let option of options" [value]="option">
{{ option }}
</mat-option>
</mat-autocomplete>
</mat-chip-list>
<mat-hint align="end">Press comma or enter after each selection</mat-hint>
</mat-form-field>
由於您尚未在輸入中添加任何控件來檢測值變化,因此無法正常工作
<input placeholder="Enter Items..."
[formControl]="displayCtrl"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add($event)" matInput [matAutocomplete]="auto"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.