1

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>

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.