简体   繁体   English

如何从.component.ts文件中* ngFor生成的输入中获取值?

[英]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文件中提供输入。

Here's a Plunker you can mess with 这是一个你可以弄乱的笨蛋

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

相关问题 来自component.ts的* ngFor访问元素 - Access element of *ngFor from component.ts 如何从下拉列表和 *ngFor 中创建的文本中获取 component.ts 中的输入值 - How to get input value in component.ts from dropdown and text created within *ngFor in table Angular - 当它们在 component.ts 文件中启动时,如何从指令内部检测表单事件? - Angular - How do I detect form events from inside a directive when they are initiated in component.ts file? 数据如何从 html 文件传输到 component.ts 文件 - How data is transferred from html file to component.ts file 我需要从我的 component.ts 文件中保存的 JSON 对象中获取数据 - I need to fetch data from a JSON object saved in my component.ts file 如何从component.ts获取Material DatePicker的选定值 - How to get selected value of Material DatePicker from component.ts 如何正确地将函数从 component.ts 移动到服务文件? - How to properly move function from component.ts to a service file? 如何从 angular 中的表单获取输入到 component.ts - How to get input from Form in angular in to component.ts 如何从component.ts文件中Angular 2中的json响应中获取键/值对 - How to get Key/Value Pairs From json Response in Angular 2 in the component.ts file Angular2:我如何从component.ts导航到子项 - Angular2: how do i navigate to children from component.ts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM