【问题标题】:Undefined button click value未定义的按钮点击值
【发布时间】:2018-09-18 12:35:46
【问题描述】:

我在尝试控制台时得到一个未定义的值。单击按钮时注销一个值

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

【问题讨论】:

    标签: angular


    【解决方案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);
      }
    

    【讨论】:

    • full standard undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined full 这是我的控制台日志,似乎它并不总是采用值/这是在 Angular 中实现它的最佳方式吗?
    【解决方案2】:

    试试

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

    【讨论】:

      【解决方案3】:

      只传值

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

      【讨论】:

      • 仍然给我未定义
      • 你有value的房产吗
      • 无属性,只是按钮中的硬编码值
      • 检查@JavascriptLover 答案
      猜你喜欢
      • 2015-06-22
      • 1970-01-01
      • 2019-10-22
      • 2018-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多