未定义的按钮点击值

时间:2018-09-18 12:35:47

标签: angular

尝试进行控制台操作时,我得到一个未定义的值。单击按钮时注销一个值

TS:

  fullView(e) {
    console.log(e);
  }

HTML:

  <button mat-button (click)="fullView(this.value)" value="full">Expand view</button>
  <button mat-button (click)="fullView(this.value)" value="standard">Expand view</button>

谁能看到原因?我只是想通过点击功能将值传递给TS文件

3 个答案:

答案 0 :(得分:1)

尝试下面的stackblitz

Stackblitz

<button mat-button (click)="fullView($event)" value="full">Expand view</button>
  <button mat-button (click)="fullView($event)" value="standard">Expand view</button>


fullView(e) {
     console.log(e);
    console.log(e.target.value);
  }

答案 1 :(得分:0)

尝试

<button mat-button (click)="fullView($event)" value="full">Expand view</button>

然后您可以访问:

  var target = $event.target || $event.srcElement || $event.currentTarget;
  var idAttr = target.attributes.id;
  var value = idAttr.nodeValue;

答案 2 :(得分:0)

只需传递值

<button mat-button (click)="fullView(value)" value="full">Expand view</button>