javascript - 如何清除 Angular 中组合框的选定值

标签 javascript html angular typescript

我有一个简单的 html 文件,其中包含 1 个组合框和 1 个清除按钮。我怎样才能使那个清除按钮在单击时清除组合框选定的值。这是我的代码:

mat-card-content fxLayout="row wrap" fxLayoutAlign="left" fxLayoutGap="30px">

          <mat-form-field fxFlex="30%">
            <mat-select placeholder="Choose Employer"
                        [(ngModel)]="customModel"
                        #activeEmployers="ngModel"
                        required>
              <mat-option *ngFor="let emp of employerList" [value]="emp.displayName">{{emp.displayName}}
              </mat-option>
            </mat-select>
          </mat-form-field>
        </mat-card-content>
        <mat-card-actions>
          <button type="button" class="get-button" (click)="getEmployers()">
            CLEAR
          </button>
        </mat-card-actions>
      </mat-card>

但是在这里,如果我使用

<button type="button" class="get-button" (click)="onClearSelected()">
                CLEAR
              </button>

在 ts 文件中:

onclear(){this.employerList=null}

然后它会清除组合框的所有选项。我怎样才能达到唯一选定的值并将其清除?

最佳答案

您需要清除[(ngModel)]的值。使用以下内容更改您的 onclear 函数

onclear(){this.customModel=null}

将雇主列表设置为未定义会清除您选择的选项。

关于javascript - 如何清除 Angular 中组合框的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54391774/

相关文章:

javascript - 如何从对象的原型(prototype)方法访问 JavaScript 对象属性

html - 段落不使用左边距移动

javascript - 如何限制与我的 Angular ui-select 链接的 10,000 个对象的数组?

html - div 内的可滚动 div

forms - Angular 4使用 react 形式在数组内创建动态formArray

angular - fixture.debugElement.componentInstance 和 fixture.nativeElement 有什么区别?

javascript - 从 dart native/flutter 调用 javascript

javascript - 如果有人尝试在未登录的情况下访问页面,如何避免崩溃?

javascript - 使用 Requirejs 的非 AMD 库

node.js - git checkout 后恢复 Angular 项目