如何重置数组中的值,OnClick 到按钮?

How to reset value from an array, OnClick to a button?

我正在使用 Tabulator 生成 table。单击 table 单元格时,我将单元格值推送到数组,数组中每个项目的初始值为“0”。我想实现一个重置按钮,它将分配的值重置为“0” onClick 到重置按钮或 window.

中的任何位置

component.ts

 names = [{name: first, val: void 0},{name: second, val: void 0}]

 tabulatorColumnName = [{title: firstTitle, field: firstField, cellClick: 
                          this.getValue.bind(this)}
                        {title: secondTitle, field: secondField, 
                          cellClick:this.getValue.bind(this)}]

假设只有一行数据,以下函数将第一个字段和第二个字段的数据推送到names[0].val 和names[1].val。

     getValue(e, row) {
          this.names[0].val = row.getData().firstField
          this.names[1].val = row.getData().secondField

     }

component.html

我想实现一个函数或按钮,当用户单击 window 中的任意位置或按钮时将清除 [(ngmodel)] 值。

 <form (ngSubmit)="saveChild(sideToggleForm)" 
  #sideToggleForm="ngForm">
  <div *ngFor="let name of names">
    <div class="{{ name.type }}">
      <small>{{ name.label }}</small>
      <mat-form-field class="full-width" appearance="outline">
        <input
          name="{{ name.name }}"
          matInput
          [(ngModel)]="name.val"
        />
      </mat-form-field>
    </div>
  </div>
</form>

在您的HTML、

中添加一个按钮
<button (click)="resetNameValues()">Reset</button>

在TS中定义函数,

resetNameValues(){
    // Iterate over name array and set val to 0
    this.names.forEach(
        name => name.val=0
    )
}