问题描述
我正在尝试创建一个表单,您可以在发送前添加多行.为此,我将数据存储在Lines数组中,并使用ngFor和ngModel将数据绑定/检索到表单中.
I'm trying to create a form where you can add multiple lines before sending it. For that I'm using storing data in an array of Lines and use ngFor and ngModel to bind / retrieve data into my form.
这是我的代码的样子
export class MyComponent implements OnInit {
lines : Line[] = [];
constructor() { }
ngOnInit() {
this.lines.push(new Line("XXXXXX1",0,0));
}
addLine(){
let line = (new Line("XXXXXX1",0,0));
this.lines.push(line);
}
save(){
console.log(this.lines);
}
}
class Line {
type: string;
qteRejet : number;
qteComm : number;
constructor(type: string = "XXXX1", qteRej: number = 0, qteComm : number = 0){
this.type = type;
this.qteRejet = qteRej;
this.qteComm = qteComm;
}
}
和表格:
<div class="form-group">
<div class="row" *ngFor="let line of lines; let ind=index;">
<div class="col-lg-1">
<i class="fa fa-2x fa-plus-square add-btn" (click)="addLine()" *ngIf="lines[lines.length-1] === line"></i>
</div>
<div class="col-lg-3">
<select class="form-control" name="type" [(ngModel)]="lines[ind].type">
<option>XXXXXX1</option>
<option>XXXXXX2</option>
<option>XXXXXX3</option>
</select>
</div>
<div class="col-lg-4">
<input class="form-control" type="number" min="0" name="qteCommande" [(ngModel)]="lines[ind].qteComm">
</div>
<div class="col-lg-4">
<input class="form-control" type="number" min="0" name="qteRejet" [(ngModel)]="lines[ind].qteRejet">
</div>
</div>
</div>
当我一次添加所有行时,该表单运行完美.现在,由于某种原因,当我尝试填充一行然后添加另一行时,表单将重置先前输入的行.
When I add all the lines at once, the form works perfectly. Now when I try to fill a line then add another line, for some reason, the form resets the lines previously entered.
但是当我检查数组时,它仍然具有输入的值
But when I check my array, it still has the entered values
有人可以解释这里发生的事情/我在做什么错吗?任何帮助将不胜感激.
Can someone explain what is going on here / what I am doing wrong ? Any help would be greatly appreciated.
如 @Ploppy 所建议,我尝试使用trackBy并将ID添加到我的行中.我还将这一行添加到表单中:
EDIT : I tried, as suggested by @Ploppy to use trackBy and added an id to my lines. I also added this line to the form :
<p>{{line.id}} | {{line.type}} | {{line.qteComm}} | {{line.qteRejet}}</p>
这就是正在发生的事情:
And this is what's happening :
窗体将重置,但值已正确绑定.
The form resets but the values are correctly binded.
推荐答案
所以我终于在代码中找出了问题所在.我的输入位于form
内,并添加新行以添加具有相同name
的输入(在Agular 2中不允许这样做).用name=attribute-{{id}}
替换我的name
属性解决了该问题.
So I finally figured out the problem in my code. My inputs were inside a form
, and adding a new line added an input with the same name
(Which is not allowed in Agular 2). Replacing my name
attributes by name=attribute-{{id}}
solved the problem.
<div class="form-group">
<div class="row" *ngFor="let line of lines; let ind=index;">
<div class="col-lg-1">
<i class="fa fa-2x fa-plus-square add-btn" (click)="addLine()" *ngIf="lines[lines.length-1] === line"></i>
</div>
<div class="col-lg-3">
<select class="form-control" name="type-{{ind}}" [(ngModel)]="lines[ind].type">
<option>XXXXXX1</option>
<option>XXXXXX2</option>
<option>XXXXXX3</option>
</select>
</div>
<div class="col-lg-4">
<input class="form-control" type="number" min="0" name="qteCommande-{{ind}}" [(ngModel)]="lines[ind].qteComm">
</div>
<div class="col-lg-4">
<input class="form-control" type="number" min="0" name="qteRejet-{{ind}}" [(ngModel)]="lines[ind].qteRejet">
</div>
</div>
</div>
这是一个汇总所有内容的柱塞: 柱塞
Here is a Plunker that summaries everything : Plunker
这篇关于ngFor和ngModel推入数组时重置输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!