html - Material Angular 自动完成 : how to remains the mat-option in the screen after selecting an option?

标签 html angular typescript angular-material

我正在使用 Material auto complete angular . 在我的 mat-option 列表中选择一个选项后,我的 mat-options 隐藏。选择后我需要保留选项。显示问题的 Gif:

enter image description here

我的 html

<div class="col-xl-6 margemColunas">
    <label>Anunciaremos nas contas *</label>
     <input class="form-control inputContasB2W"
          #inputContasB2W
          [formControl]="contasB2WControl"
          [matAutocomplete]="auto"
          (matChipInputTokenEnd)="add($event)">
      <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event, i)">
        <mat-option *ngFor="let contaB2W of contasFiltradas | async" [value]="contaB2W">
          {{contaB2W}}
        </mat-option>
      </mat-autocomplete>
</div>

<div class="col-xl-6 alinhaChips">
  <mat-chip-list>
    <mat-chip
    *ngFor="let contaB2W of produto.contasAnunciarB2W; let j = index"
    [selectable]="selected"
    [removable]="removable"
    (removed)="removeTag(i, j)">
    {{contaB2W}}
    <mat-icon matChipRemove><i class="fa fa-close"></i></mat-icon>
  </mat-chip>
  </mat-chip-list>
</div>

我的问题:

constructor(){
    this.contasFiltradas = this.contasB2WControl.valueChanges.pipe(
      startWith(null),
      map((nomeConta: string | null) => nomeConta ? this._filter(nomeConta) : this.contasB2W.slice()));
}

  private _filter(value: string): string[] {
    const filterValue = value.toLowerCase();
    return this.contasB2W.filter(conta => conta.toLowerCase().indexOf(filterValue) === 0);
  }

selected(event: MatAutocompleteSelectedEvent, index: number): void {

 this.produtosConfirmadosAnuncio[index].contasAnunciarB2W.push(event.option.viewValue);

    this.inputContasB2W.nativeElement.value = '';

          } 
        }

最佳答案

我使用了 blur() 的变通方法,然后再次使用 focus() 输入。

在 ts:

@ViewChild('inputContasB2W') autocompleteInput: ElementRef;

selected(event: MatAutocompleteSelectedEvent) {
  [..do your stuff...]

  this.autocompleteInput.nativeElement.blur();
  setTimeout(() => {
      this.autocompleteInput.nativeElement.focus();
    }, 100
  );
}

如果您想保留输入文本,您必须将其保存在 _filter() 中,并在 blur() 之前再次设置值,例如 this.contasB2WControl。 setValue(this.lastFilterValue);

关于html - Material Angular 自动完成 : how to remains the mat-option in the screen after selecting an option?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54748394/

相关文章:

javascript 无法在 struts html 标签上工作

javascript - Angular 2+ 多重计算和多重结果

node.js - Node JS Web 应用程序部署

typescript - 直接从 npm 包导入 Typescript/JSX 代码

javascript - 无法读取未定义的属性 'insertOnMatch' - TypeScript

javascript - JS动画会覆盖自身而不是替换

html - 捕获 anchor 标签上的按键

javascript - 用于检测和替换 undefined object 属性的全局函数/服务 | Angular 2-TS

javascript - 获取由内容元素选择的 dom-repeat 模板生成的元素

javascript - 如何处理 Angular 数据表中的列不匹配?