英文:
Form with iterable field
问题
I'm creating a form with some fields can be repeated:
我的表单中有一些可以重复的字段:
my form:
我的表单:
newSurvey: any = {
surveyName: '',
surveyOwner: '',
questions: [
{ questionTitle: '', values: ['', '', '', '', ''] }
]
};
I have created this structure for the form, where questions can be more than one. And every questions have a question Title and Values.
我已经为表单创建了这个结构,其中问题可以有多个。每个问题都有一个问题标题和值。
This is the form:
这是表单:
<form (ngSubmit)="createSurvey()">
<div class="modal-body">
<label for="surveyName">Name</label>
<input type="text" id="surveyName" [(ngModel)]="newSurvey.surveyName" name="surveyName" required>
</div>
.........
<div *ngFor="let question of newSurvey.questions; let i = index" class="question">
<label for="question{{i}}">Question #{{i+1}}:</label>
<input type="text" id="question{{i}}" [(ngModel)]="question.questionTitle" name="question" required>
<label for="values{{i}}">Values: </label>
<div>
<input type="text" *ngFor="let value of question.values; let j = index" [(ngModel)]="question.values[j]"
name="value" id="value" required>
</div>
</div>
<button type="button" (click)="addQuestion()">Add question</button>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" type="submit">Create survey</button>
</div>
</form>
Now my problem is that when I insert a value in a "Values" Field the same values is wrote in other field.
现在我的问题是,当我在一个“Values”字段中插入一个值时,相同的值也被写入其他字段。
How can I resolve? Thank you.
我应该如何解决?谢谢。
英文:
I'm creating a form with some fields can be repeated:
my form:
newSurvey: any = {
surveyName: '',
surveyOwner: '',
questions: [
{ questionTitle: '', values: ['', '', '', '', ''] }
]
};
I have created this structure for the form, where questions can be more than one. And every questions have a question Title and Values
This is the form:
<form (ngSubmit)="createSurvey()">
<div class="modal-body">
<label for="surveyName">Name</label>
<input type="text" id="surveyName" [(ngModel)]="newSurvey.surveyName" name="surveyName" required>
</div>
........
<div *ngFor="let question of newSurvey.questions; let i = index" class="question">
<label for="question{{i}}">Question #{{i+1}}:</label>
<input type="text" id="question{{i}}" [(ngModel)]="question.question" name="question" required>
<label for="values{{i}}">Values: </label>
<div>
<input type="text" *ngFor="let value of question.values; let j = index" [(ngModel)]="question.values[j]"
name="value" id="value" required>
</div>
</div>
<button type="button" (click)="addQuestion()">Add question</button>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" type="submit">Create survey</button>
</div>
</form>
Now my problem is that when I insert a value in a "Values" Field the same values is wrote in other field.
How can I resolve? Thank you
答案1
得分: 2
问题是 *ngFor
在每次变化时都会重新渲染,为了避免这个问题,您可以实现 trackBy
。
<input
type="text"
*ngFor="let value of question.values; let j = index; trackBy:trackBy"
[(ngModel)]="question.values[j]"
name="value"
id="value"
required
/>
trackBy(index: number): number {
return index;
}
英文:
The problem is that *ngFor
renders at every change, to avoid this you can implement tackBy
<input
type="text"
*ngFor="let value of question.values; let j = index; trackBy:trackBy"
[(ngModel)]="question.values[j]"
name="value"
id="value"
required
/>
trackBy(index: number): number {
return index;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论