【问题标题】:Can't add element by the button value in angular 5无法通过角度 5 中的按钮值添加元素
【发布时间】:2018-10-09 15:16:59
【问题描述】:

我想将按钮的值添加到我的程序中。

首先我展示一些按钮。

<button *ngFor="let row of buttonTable.rows" mat-raised-button class="btn btn-sm" value={{row}} (click)="addLab(this.value)">{{row}}</button>

我的 addLab() 方法是

  addLab(value:any):void{
  this.formTable.rows.push([value]);
  }

而我的buttonTable是这样的。

declare interface oneLineTable {
  rows: string[][];
}

但我无法通过这种点击方法获得价值。有人有什么想法吗?

【问题讨论】:

    标签: angular forms button click


    【解决方案1】:

    也许您应该尝试在 addLab 点击事件中传递 'row' 而不是 'this.value':

    <button *ngFor="let row of buttonTable.rows" mat-raised-button class="btn btn- sm" (click)="addLab(row)">{{row}}</button>
    

    【讨论】:

    • 太棒了! :) 如果您同意,请投票支持我的答案并验证为最佳答案!谢谢,意义重大!
    【解决方案2】:

    试试这个 [value]="row" 并在你的点击方法中传递 $event

    <button *ngFor="let row of buttonTable.rows" mat-raised-button class="btn btn-sm" [value]="row" (click)="addLab($event)">{{row}}</button>
    

    得到这样的值

    addLab(event) {
     console.log(event.target.value);
     }
    

    【讨论】:

      猜你喜欢
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 2012-08-06
      • 2021-03-03
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      相关资源
      最近更新 更多