简体   繁体   English

如何在Angular 4中获取单击按钮的值

[英]How to get the value of the button on click in Angular 4

I have 4 buttons each with unique value.I am getting undefined in console log with current code. 我有4个按钮,每个按钮都有唯一的值。

HTML HTML

<button type="submit" class="parcel-btn weight-option" value="25" (click)="onItemSelector()">
<button type="submit" class="parcel-btn weight-option" value="50" (click)="onItemSelector()">
<button type="submit" class="parcel-btn weight-option" value="250" (click)="onItemSelector()">
<button type="submit" class="parcel-btn weight-option" value="100" (click)="onItemSelector()">

Typescript 打字稿

onItemSelector() {
  const weightSelector = this.elm.nativeElement.querySelectorAll('.weight-option').value;
  console.log(weightSelector);
}

Try this 尝试这个

Pass the value to the onItemSelector function 将该值传递给onItemSelector函数

   <button type="submit" class="parcel-btn weight-option" (click)="onItemSelector(25)">

Get the value in ts 获取以ts为单位的值

onItemSelector(value :any) {
console.log(value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM