微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

单击“清除”按钮后,Mat Auto Complete 不会打开下拉面板

如何解决单击“清除”按钮后,Mat Auto Complete 不会打开下拉面板

我在 Angular 项目中有自定义的 Mat Auto Complete。它有 2 个后缀,Clear 和 Dropdown 按钮。当我点击清除按钮代码 this.myControl.reset('',{ emitEvent: true }); 重置输入值。但是下拉面板没有打开。我尝试使用以下格式,但没有任何效果

  1. this.myControl.reset();
  2. this.myControl.reset('',{ emitEvent: true });
  3. this.myControl.patchValue('');

StackBlitz Demo

autocomplete-display-example.ts

.......

 clearinput() {
    this.arrowIcon = "arrow_drop_down";
    this.myControl.reset('',{ emitEvent: true });
  }

......

autocomplete-display-example.html

<form class="example-form">
 <mat-form-field class="example-full-width">
   <mat-label>Assignee</mat-label>
   <input
     type="text"
     matInput
     [formControl]="myControl"
     [matAutocomplete]="auto"

   />
   <div matSuffix style="display:flex">
     <button
       *ngIf="myControl!==undefined && myControl.value!==null && myControl?.value!==''"
       mat-icon-button
       aria-label="Clear"
       (click)="clearinput()"
       type="button"
     >
       <mat-icon>clear</mat-icon>
     </button>

     <button mat-icon-button aria-label="Clear" type="button">
       <mat-icon>{{arrowIcon}}</mat-icon>
     </button>
   </div>

   <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn" (opened)="arrowIcon='arrow_drop_up'"
     (closed)="arrowIcon='arrow_drop_down'" (optionSelected)="arrowIcon='arrow_drop_down'">
     <mat-option *ngFor="let option of filteredOptions | async" [value]="option">
       {{option.name}}
     </mat-option>
   </mat-autocomplete>
 </mat-form-field>
</form>

<!-- copyright 2020 Google LLC. All Rights Reserved.
   Use of this source code is governed by an MIT-style license that
   can be found in the LICENSE file at http://angular.io/license -->

解决方法

好的,当我点击清除图标时,它会重置表单控件并关闭面板。为了解决这个问题,我们可以手动打开面板。将 MatAutocompleteTriggerevent

一起注入到方法中

查看更新的StackBlitz Demo

  openPanel(evt: any,trigger: MatAutocompleteTrigger): void {
    evt.stopPropagation();
    this.myControl?.reset();
    trigger.openPanel();
    this.inputAutoComplete?.nativeElement.focus();
  }

#trigger="matAutocompleteTrigger" 添加到输入并将其传递给按钮 click() 方法

autocomplete-display-example.html


<form class="example-form">
  <mat-form-field class="example-full-width">
    <mat-label>Assignee</mat-label>
    <input #inputAutoComplete
           [formControl]="myControl"
           [matAutocomplete]="auto"
           #trigger="matAutocompleteTrigger"
           matInput
           type="text"
    />

    <div matSuffix style="display:flex">
      <button
        (click)="openPanel($event,trigger)"
        *ngIf=" myControl?.value!==null && myControl?.value!==''"
        aria-label="Clear"
        mat-icon-button
        type="button"
      >
        <mat-icon>clear</mat-icon>
      </button>

      <button aria-label="Clear" mat-icon-button type="button">
        <mat-icon>{{arrowIconSubject.getValue()}}</mat-icon>
      </button>
    </div>

    <mat-autocomplete #auto="matAutocomplete" (closed)="arrowIconSubject.next('arrow_drop_down')"
      (opened)="arrowIconSubject.next('arrow_drop_up')" (optionSelected)="arrowIconSubject.next('arrow_drop_down')"
      [displayWith]="displayFn">
      <mat-option *ngFor="let option of filteredOptions | async " [value]="option">
        {{option.name}}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</form>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?