[英]How do I get the values from the inputs that *ngFor generate in my .component.ts file?
As said in the title, I want to get the values from my generated inputs. 如标题中所述,我想从生成的输入中获取值。 How can I do that so I can work on them in my
.ts
file? 我该怎么做,以便可以在
.ts
文件中对其进行处理?
Here is the code so far for my blabla.component.html
: 到目前为止,这是我的
blabla.component.html
的代码:
<form #f="ngForm" (ngSubmit)="onSubmit(f)" class="form-horizontal">
<input name="codeB" ngModel #codeB="ngModel" id="codeB" type="text" class="form-control frm" placeholder="Code Barre" />
<input name="name" ngModel #name="ngModel" id="name" type="text" class="form-control frm" placeholder="Name Produit" />
<table class="table table-striped">
<tr *ngFor="let mag of listMagasins | async">
<td>
<h4>{{mag.name}}</h4>
</td>
<td>
<input name="prix" ngModel #prix="ngModel" id="prix" type="text"
class="form-control frm" placeholder="Price product" required />
</td>
</tr>
</table>
<!--<input name="nameMag" ngModel #nameMag="ngModel" id="nameMag" type="text"
class="form-control frm" placeholder="Magasin" />-->
<button class="btn btn-primary" type="submit">Ajouter</button>
</form>
Thank you for the help! 感谢您的帮助!
This can be achieved by naming things uniquely instead of naming each input prix
and, if we want the values in the TS, using ViewChildren
: 这可以通过唯一地命名事物而不是命名每个输入
prix
来实现,如果我们想要TS中的值,可以使用ViewChildren
:
component.html component.html
<tr *ngFor="let mag of listMagasins | async; let i = index;">
<td>
<h4>{{mag.name}}</h4>
</td>
<td>
<input name="prix{{i}}" ngModel #prix="ngModel" id="prix" type="text"
class="form-control frm" placeholder="Price product" required />
</td>
</tr>
component.ts component.ts
import {ViewChildren} from "@angular/core"
export class ComponentClass {
@ViewChildren('prix') inputs;
<...>
}
Which will set our form up to have separately bound controls and give us the inputs in the TS file. 这将使我们的表单具有单独绑定的控件,并在TS文件中提供输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.