首页 > 解决方案 > How do we clear an angular dropdown list value in angular?

问题描述

How do we clear an angular dropdown list value in angular ? clear the selection with an x button or clear button ? Thank You.

Code

<div fxFlex fxLayout="row" formGroupName="people">
          <mat-form-field appearance="outline" class="pr-4" fxFlex>
            <mat-label>People</mat-label>
            <mat-select formControlName="people">
              <mat-option *ngFor="let people of Peopls" [value]="peope.value">
                {{ people.literal }}
              </mat-option>
            </mat-select>
          </mat-form-field>

 <button mat-button *ngIf="" matSuffix mat-icon-button 
    aria-label="Clear" (click)="";>
  <mat-icon>close</mat-icon>

        </div>
      </div>

标签: javascriptangularjsangulartypescript

解决方案


尝试设置selectedValue.value=undefined">点击事件。

例如

<div >
          <mat-form-field>
            <mat-label>People</mat-label>
            <mat-select #selectedValue >
              <mat-option *ngFor="let p of people" [value]="p.value">
                {{ p.value }}
              </mat-option>
            </mat-select>
          </mat-form-field>
 <button mat-button (click)="selectedValue.value=undefined">X</button>
 </div>

工作演示: https ://stackblitz.com/edit/angular-jmzfsc


推荐阅读