文本字段在向数组添加新元素时重置为相同的值

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

Text fields reset to same value on adding new element to array

问题

我有一个名为courses的变量,它是一个对象数组。
在模板中,ngFor指令输出了这个数组的内容。

我的模板:

<div>
  <div class="row" *ngFor="let relacao of courses; let i = index">
    <div>
      <input 
          type="text" 
          name="courses[i].label" 
          [(ngModel)]="courses[i].label"
      />
    </div>
  </div>
</div>
		
<div>
  <button (click)="addCourse()">添加</button>
</div>

我的TypeScript:

addCourse() {
  this.courses.push({ label: 'test' });
}

当我使用更改数组元素的值并向数组添加新项时,它会将所有标签重置为初始值(null)。

我需要输入值在向数组添加新记录时不被重置。有人可以帮助我吗?

英文:

I have a variable called courses which is an array of objects.
In the template, an ngFor outputs the content of this array.

my template:

&lt;div&gt;
  &lt;div class=&quot;row&quot; *ngFor=&quot;let relacao of courses let i = index&quot;&gt;
    &lt;div&gt;
      &lt;input 
          type=&quot;text&quot; 
          name=&quot;courses[i].label&quot; 
          [(ngModel)]=&quot;courses[i].label&quot;
      /&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
		
&lt;div&gt;
  &lt;button (click)=&quot;addCourse()&quot;&gt;add&lt;/button&gt;
&lt;/div&gt;

my ts:

addCourse() {
  this.courses.push({ label: &#39;test&#39; });
}

When i change the value of an array element using the <input>and add a new item to the array, it resets all the labels to the initial value (null).

文本字段在向数组添加新元素时重置为相同的值

I need the input value not to be reset when adding a new record to the array. Can someone help me?

答案1

得分: 1

确切的代码似乎对我来说运行正常。

我仍然分享代码,请检查是否有任何差异。

<div>
  <div class="row" *ngFor="let relacao of courses; let i = index">
    <div>
      <input
        type="text"
        name="courses[i].label"
        [(ngModel)]="courses[i].label"
      />
    </div>
  </div>
</div>

<div>
  <button (click)="addCourse()">add</button>
</div>
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  courses: { label: string }[] = [];

  addCourse() {
    this.courses.push({ label: 'test' });
  }
}
英文:

The exact code seems to be working fine for me.

I am still sharing the code please check if there are any differences

&lt;div&gt;
  &lt;div class=&quot;row&quot; *ngFor=&quot;let relacao of courses; let i = index&quot;&gt;
    &lt;div&gt;
      &lt;input
        type=&quot;text&quot;
        name=&quot;courses[i].label&quot;
        [(ngModel)]=&quot;courses[i].label&quot;
      /&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div&gt;
  &lt;button (click)=&quot;addCourse()&quot;&gt;add&lt;/button&gt;
&lt;/div&gt;
import { Component } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-root&#39;,
  templateUrl: &#39;./app.component.html&#39;,
  styleUrls: [&#39;./app.component.css&#39;],
})
export class AppComponent {
  courses: { label: string }[] = [];

  addCourse() {
    this.courses.push({ label: &#39;test&#39; });
  }
}

答案2

得分: 0

I found the problem accidentally. Adding {{ i }} to the input name solved the problem.

<div>
  <div class="row" *ngFor="let relacao of courses; let i = index">
    <div>
      <input 
        type="text" 
        name="courses[{{i}}].label" 
        [(ngModel)]="courses[i].label"
      />
    </div>
  </div>
</div>

Thank you all!

英文:

accidentally i found the problem. adding {{ i }} to the input name solved the problem

&lt;div&gt;
  &lt;div class=&quot;row&quot; *ngFor=&quot;let relacao of courses let i = index&quot;&gt;
   &lt;div&gt;
    &lt;input 
      type=&quot;text&quot; 
      name=&quot;courses[{{i}}].label&quot; 
      [(ngModel)]=&quot;courses[i].label&quot;
    /&gt;
   &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

thank you all!

huangapple
  • 本文由 发表于 2023年7月20日 11:44:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726537.html
匿名

发表评论

匿名网友

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

确定