简体   繁体   English

如何获取* ngFor循环中输入字段的值?

[英]how to obtain the value of an input field in an *ngFor loop?

I need to obtain values from a series of inputs in an *ngFor loop. 我需要从* ngFor循环中的一系列输入中获取值。 I tried using the name "(change)=addQty(qty,i)" I tried 2way binding but all the inputs ended up with the same value. 我尝试使用名称“(change)= addQty(qty,i)”,尝试2way绑定,但所有输入最终都具有相同的值。 I tried searching Google. 我尝试搜索Google。 In the code that follows I have stripped out some non-related code and classes. 在下面的代码中,我删除了一些不相关的代码和类。

<div *ngFor='let meal of meals; let i = index'>
  <div>
    <span>
      {{meal.mealName}} &emsp;
      <mat-icon>thumb_up</mat-icon> {{meal.dislike}} &nbsp;
      <mat-icon>thumb_down</mat-icon>{{meal.like}}
    </span>
  </div>
  <br>

  <div>
    {{meal.mealDesc}}
    <br> Price: {{meal.price1 | currency}} &emsp; Qty:
    <input class='number' type='number' name='qty' min=0 (change)=addQty(i)>
  </div>
</div>

If it is something simple I will be so embarrassed. 如果这很简单,我会很尴尬。

您没有将change事件的值传递回控制器,而是将addQty($event, i)更改为addQty($event, i) ,在控制器addQty(e, i)e.target.datae.target.value

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

相关问题 如何基于另一个字段值禁用ngFor中的特定输入字段 - How to disable a specific input field in ngFor based on another field value 如何从循环ngFor中的输入类型=“文本”中获取输入值 - How to get input value from input type=“text” in loop ngFor 如何使用输入字段过滤Angular 4中*ngFor循环内的项目 - How to filter items inside *ngFor loop in Angular 4 using input field Angular - 如何通过一种方式绑定在 ngFor 循环中获取输入值 - Angular - How to get input value at ngFor loop with one way binding 如何从 *ngFor 循环中的输入中获取值 - How to take value from input which is in *ngFor loop 如何在Angular2中设置动态#id(* ngFor)并从中获取值? - How to set dynamic #id(*ngFor) and obtain the value from it in Angular2? 仅在Angular中打开使用ngFor循环创建的特定输入字段 - Open only specific input field created with ngFor loop in Angular 如何将* ngFor循环变量的值分配给角动态组件的@Input属性? - How to assign value of *ngFor loop variable to angular dynamic component's @Input property? 在父组件中单击按钮时如何更改子组件的@input值(子组件位于* ngfor循环中) - How to change the @input value of child component when button click in parent component ( child component is in *ngfor loop ) 如何在循环外得到* ngFor的索引值? - How to get the value of the index of *ngFor outside the loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM