表单与可迭代字段

huangapple go评论64阅读模式
英文:

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: &#39;&#39;,
    surveyOwner: &#39;&#39;,
    questions: [
      { questionTitle: &#39;&#39;, values: [&#39;&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;] }
    ]
  };

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:

&lt;form (ngSubmit)=&quot;createSurvey()&quot;&gt;
        &lt;div class=&quot;modal-body&quot;&gt;
            &lt;label for=&quot;surveyName&quot;&gt;Name&lt;/label&gt;
            &lt;input type=&quot;text&quot; id=&quot;surveyName&quot; [(ngModel)]=&quot;newSurvey.surveyName&quot; name=&quot;surveyName&quot; required&gt;
        &lt;/div&gt;

       ........

        &lt;div *ngFor=&quot;let question of newSurvey.questions; let i = index&quot; class=&quot;question&quot;&gt;
            &lt;label for=&quot;question{{i}}&quot;&gt;Question #{{i+1}}:&lt;/label&gt;
            &lt;input type=&quot;text&quot; id=&quot;question{{i}}&quot; [(ngModel)]=&quot;question.question&quot; name=&quot;question&quot; required&gt;

         
            &lt;label for=&quot;values{{i}}&quot;&gt;Values: &lt;/label&gt;
            &lt;div&gt;
                &lt;input type=&quot;text&quot; *ngFor=&quot;let value of question.values; let j = index&quot; [(ngModel)]=&quot;question.values[j]&quot;
                    name=&quot;value&quot; id=&quot;value&quot; required&gt;
            &lt;/div&gt;

        &lt;/div&gt;

        &lt;button type=&quot;button&quot; (click)=&quot;addQuestion()&quot;&gt;Add question&lt;/button&gt;

        &lt;div class=&quot;modal-footer&quot;&gt;
            &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-dark&quot; type=&quot;submit&quot;&gt;Create survey&lt;/button&gt;
        &lt;/div&gt;
    &lt;/form&gt;

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

&lt;input
  type=&quot;text&quot;
  *ngFor=&quot;let value of question.values; let j = index; trackBy:trackBy&quot;
  [(ngModel)]=&quot;question.values[j]&quot;
  name=&quot;value&quot;
  id=&quot;value&quot;
  required
/&gt;
trackBy(index: number): number {
  return index;
}

huangapple
  • 本文由 发表于 2023年6月12日 16:09:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定