簡體   English   中英

我們如何清除 angular 中的 angular 下拉列表值?

[英]How do we clear an angular dropdown list value in angular?

我們如何清除 angular 中的 angular 下拉列表值? 使用 x 按鈕或清除按鈕清除選擇? 謝謝你。

代碼

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

嘗試在點擊事件上設置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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM