【发布时间】:2020-06-05 10:08:01
【问题描述】:
我的下拉列表显示了所有用户过去创建的现有标签/芯片,但我不确定为什么当我从下拉列表中选择一个时,该项目没有显示在我的输入字段中(就像图表标签一样马上)。我可以通过输入文本并按 Enter 来创建芯片/标签,但我还想要用户可以从下拉列表中选择某人创建的标签的功能。
图表标签是我通过输入创建的示例(未从列表中选择)
如果有人能给我建议或帮助我,我将不胜感激。
TS
selected(event: MatAutocompleteSelectedEvent): void {
const tag = {tag: event.option.viewValue, type: TagType.user};
this.normalTags.push(tag);
if(this.input){
this.input.nativeElement.value = "";
}
this.tagCtrl2.setValue(null);
//remove the selected one from the option list
var index = this.allTagNames.indexOf(tag.tag);
this.allTagNames.splice(index, 1);
this.mapper();
}
mapper(){
this.filteredTags = this.tagCtrl2.valueChanges.pipe(
startWith(null),
map((tag: string | null) => tag ? this._filter(tag) : this.allTagNames.slice()))
}
HTML
<mat-form-field class="demo-chip-list" appearance="fill">
<mat-chip-list #chipList>
<mat-chip *ngFor="let chip of chips" [selectable]="selectable" [removable]="removable" (removed)="removeTags(chip)">
{{chip.tag}}
<mat-icon matChipRemove *ngIf="chip.pending !== true && removable" matTooltip="Remove a tag">cancel</mat-icon>
<mat-spinner *ngIf="chip.pending === true" [diameter]="16" mode="indeterminate"></mat-spinner>
</mat-chip>
<input matInput #input [(ngModel)]="tagIn" placeholder="Select or Create a tag" [formControl]="tagCtrl2" [matAutocomplete]="auto"
(focusout)="hideTagInput()" (keyup.enter)="addTag()"(keyup.escape)="hideTagInput()"
(keydown.backspace)="$event.stopPropagation();" (keydown.space)="$event.stopPropagation();" [matChipInputFor]="chipList" />
</mat-chip-list>
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
<mat-option *ngFor="let tag of filteredTags | async" [value]="tag">
{{tag}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
【问题讨论】:
-
您能否提供将标签添加到 HTML 的代码?和函数映射器?
-
@BrenoAntunes 嗨.. 我只是在上面添加了更多代码,我还在这里stackblitz.com/edit/angular-ivy-ynuoq9 上传了 stackblitz 中的整个代码。请告诉我..谢谢
标签: html angular typescript angular-material