【问题标题】:how to select from drop down list in Angular如何从 Angular 的下拉列表中进行选择
【发布时间】: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


【解决方案1】:

首先,您似乎在同一个输入中同时使用了反应式表单和模板驱动表单。如果不是必需的,我会考虑以这种方式删除并离开(反应式表单):

<input matInput 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" />

(如果您更改,将需要重构您的 component.ts)

但是回答你的问题:

在所选功能上,您没有将标签添加到芯片数组中,芯片数组是您用来显示标签的数组,因此您需要更改为:

selected(event: MatAutocompleteSelectedEvent) : void {
    const tag = {tag: event.option.viewValue, type: TagType.user};
    this.normalTags.push(tag);
    this.chips.push(tag); <---- new line
    if(this.input){
      this.input.nativeElement.value = "";
    }
    this.tagCtrl2.setValue(null);
    var index = this.allTagNames.indexOf(tag.tag);
    this.allTagNames.splice(index, 1);
    this.mapper();
  }

【讨论】:

  • 感谢您为我提供此信息,但请您帮我解决此问题。自从我从同事那里接手这项任务后,我已经被困了几天,完全迷路了。
  • 帮你修复什么?我用您需要添加的行更新了我的答案,如果它解决了问题,请告诉我
  • 你能帮我看看component.ts文件吗?我只是添加了这些代码,现在它会在我选择时显示,但实际上并没有存储。当我引用浏览器时,芯片不见了。而且我不能再通过输入它来创建新芯片了。如果您能帮助我,我将不胜感激。请
  • 它没有持久化数据,因为它只是存储在一个数组中。我无权访问您的所有项目,但您有一项服务可以添加您需要调用此服务并传递新标签的标签。我不知道哪个组件正在从“tagAdded.emit”接收事件,但可能是从那里调用服务。如果它没有调用服务,那么您将需要从组件调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
相关资源
最近更新 更多