简体   繁体   English

Angular - ngFor 循环上的双向绑定问题

[英]Angular - two-way binding problem on ngFor loop

I am writing a project in Angular 10 and I don't know why but the property displayed through input is different than the same property displayed using interpolation in the ngFor loop.我正在 Angular 10 中编写一个项目,但我不知道为什么,但通过输入显示的属性与在 ngFor 循环中使用插值显示的相同属性不同。 Those values start to be different every time I am pushing a new customSection to the customSections array.每次我将新的 customSection 推送到 customSections 数组时,这些值开始不同。

html part: html部分:

  <div
    class="uk-margin-top"
    *ngFor="
      let customSection of resume.customSections;
      let customSectionIndex = index
    "
  >
      <input
        class="uk-input uk-form-blank uk-text-bold uk-text-lead uk-padding-remove-left"
        type="text"
        name="sectionName"
        [(ngModel)]="customSection.sectionName
        "
        (ngModelChange)="getHtmlContent()"
      />

      <span>{{ customSection.sectionName }}</span>

typescript part:打字稿部分:

  onCustomSectionAddClick() {
    this.resume.customSections.push(new CustomSection("Untitled"));
    this.getHtmlContent();
  }

Everything is fine but when I click the button to add a new custom section then as you can see 'Untitled' is the value displayed in input for new and older (earlier added) custom sections, but span shows the other value (on this screenshot - value 'b').一切都很好,但是当我单击按钮添加新的自定义部分时,如您所见,“无标题”是在新的和旧的(较早添加的)自定义部分的输入中显示的值,但跨度显示另一个值(在此屏幕截图中) - 值“b”)。 Does anyone know why it can happen?有谁知道为什么会发生?

在此处输入图片说明

I did some workaround to make it work correctly, but still I know that it's just hack (not good solution):我做了一些解决方法以使其正常工作,但我仍然知道这只是 hack(不是好的解决方案):

html side: html端:

      <input
        class="uk-input uk-form-blank uk-text-bold uk-text-lead uk-padding-remove-left"
        type="text"
        name="sectionName"
        (change)="onCustomSectionNameChange($event, customSectionIndex)"
        value="{{ customSection.sectionName }}"
      />

      <span>{{ customSection.sectionName }}</span>

typescript side:打字稿方面:

  onCustomSectionNameChange(event: any, index: number) {
    this.resume.customSections[index].sectionName = event.target.value;
    this.getHtmlContent();
  }

After a few hours of thinking about this problem, I noticed what a simple mistake I've made.经过几个小时的思考这个问题,我注意到我犯了一个多么简单的错误。 The input's name attribute should be unique in ngFor loop.输入的 name 属性在 ngFor 循环中应该是唯一的。 So I've changed this:所以我改变了这个:

  <input
    class="uk-input uk-form-blank uk-text-bold uk-text-lead uk-padding-remove-left"
    type="text"
    name="sectionName"
    [(ngModel)]="customSection.sectionName
    "
    (ngModelChange)="getHtmlContent()"
  />

to this:对此:

  <input
    class="uk-input uk-form-blank uk-text-bold uk-text-lead uk-padding-remove-left"
    type="text"
    name="sectionName-{{ customSectionIndex }}"
    [(ngModel)]="customSection.sectionName
    "
    (ngModelChange)="getHtmlContent()"
  />

And now everything works great.现在一切都很好。 Thank you guys for trying to help me and find a mistake.谢谢你们试图帮助我并发现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM