Angular ngModelChange 显示 API json 数据中的重复值

问题描述 投票:0回答:0

更新:我已经在 Stackblitz 上为那些想要与代码交互的人重现了该错误。我有以下 JSON 数据:

related_contacts: [
    {
        "id": 1,
        "relationship": "Brother",
        "date_created": "2023-07-24T22:40:16.497590+03:00",
        "date_modified": "2023-07-24T22:40:16.497590+03:00",
        "case": 170,
        "created_by": 3,
        "tenant": 2
    },
    {
        "id": 2,
        "relationship": "Wife",
        "date_created": "2023-07-24T22:40:16.507891+03:00",
        "date_modified": "2023-07-24T22:40:16.507891+03:00",
        "case": 170,
        "created_by": 3,
        "tenant": 2
    }
]

我正在尝试使用关系值填充输入文本字段。

<div class="row py-3" *ngFor="let contact of case.related_contacts">
  <ng-container *ngIf="case.related_contacts.length > 1">
    <!--begin::Col-->
    <div class="col-lg-4">
      <!--begin::Label-->
      <label class="form-label text-dark fs-6">Relationship
      </label>
      <!--end::Label-->
      <!--begin::Input group-->
      <div class="form-floating mb-5">
        <input type="text" class="form-control" id="{{ contact.relationship }}" placeholder="Relationship" [ngModel]="contact.relationship" (ngModelChange)="testChange($event)" name="{{ contact.relationship }}" formControlName="relationship" autocomplete="off"/>
        <label for="state">Relationship</label>
      </div>
      <!--end::Input group-->
    </div>
    <!--end::Col-->
  </ng-container>
</div>

我使用 testChange 函数来查看该值。

testChange($event: any) {
  console.log($event);
},

字段中有重复。

控制台日志也显示重复的

对此有任何帮助,我们将不胜感激。

angular ngfor angular-ngmodel ngmodel
© www.soinside.com 2019 - 2024. All rights reserved.