How do we clear an angular dropdown list value in angular?

Issue

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>

Solution

Try setting selectedValue.value=undefined"> on click event.

e.g

<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>

Working Demo:
https://stackblitz.com/edit/angular-jmzfsc

Answered By – Ganesh Thorat

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published