使用选定的行角动力阵列填充输入字段
当我在列表中选择值时,为什么我的第二行从第一行中伸出相同的值?
当第二行添加时,一切都可以,但是它会使我避免使用相同的阀门。因为当我尝试输入一些值时,它可以。
html
<form [formGroup]="Form">
<div formArrayName="ItemRec">
<table>
<thead>
<tr>
<th>FORM</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let ItemRec of ItemRec.controls; let i = index;"
[formGroupName]="i">
<td (click)="removeRow(i)" style="width:100px" align="center">
<i class="fa fa-trash fa-2x"></i></td>
<td>
<input style="width:200px" class="form-control"
formControlName="dr" ngModel={{transactionCode}}>
</td>
</tr>
</tbody>
</table>
</form>
.ts
this.Form = this.fb.group({
Id:['0'],
ItemRec: this.fb.array([this.CreateItemRec()])
})
}
CreateItemRec(): FormGroup {
return this.fb.group({
id:[0],
dr: new FormControl('',Validators.required),
})
}
get ItemRec(): FormArray {
return <FormArray>this.Form.get('ItemRec') as
FormArray
}
addRow(): void {
this.ItemRec?.push(this.CreateItemRec());
Why my second row gaves me same values from my first row when i selected value in my list?
all okay when the second row added it gaves me some same val its weird though. cause when i tried to input some value its okay.
HTML
<form [formGroup]="Form">
<div formArrayName="ItemRec">
<table>
<thead>
<tr>
<th>FORM</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let ItemRec of ItemRec.controls; let i = index;"
[formGroupName]="i">
<td (click)="removeRow(i)" style="width:100px" align="center">
<i class="fa fa-trash fa-2x"></i></td>
<td>
<input style="width:200px" class="form-control"
formControlName="dr" ngModel={{transactionCode}}>
</td>
</tr>
</tbody>
</table>
</form>
.TS
this.Form = this.fb.group({
Id:['0'],
ItemRec: this.fb.array([this.CreateItemRec()])
})
}
CreateItemRec(): FormGroup {
return this.fb.group({
id:[0],
dr: new FormControl('',Validators.required),
})
}
get ItemRec(): FormArray {
return <FormArray>this.Form.get('ItemRec') as
FormArray
}
addRow(): void {
this.ItemRec?.push(this.CreateItemRec());
The list of my value is in the modal inside of that is mat table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将模板驱动的形式与反应性形式混合。这可能会有意外的行为。请删除NGMODEL。
我想它将其设置为NGFOR创建的每个字段的默认值。如果应该是默认值,请在形式创建中设置它。
You are mixing template driven forms with reactive forms. That can have unexpected behavior. Kindly remove the ngModel.
I guess it is set as default value for every field created by ngFor. If it is supposed to be the default value, set it at form creation.