简体   繁体   English

使用ngFor与ngModel动态数据的错误行为

[英]Using ngFor with ngModel dynamic data wrong behaviour

I have complex and probably rookie bug in logic of my application, so I'll try to give comprehensive amount of information. 我在应用程序的逻辑中有复杂的,可能是新手的bug,所以我会尝试提供全面的信息。

I have registration form binded to my data model. 我有一个绑定到我的数据模型的注册表单。 Phone number fields can be added by user during registration and saved as array. 用户可以在注册期间添加电话号码字段并保存为阵列。

My model: 我的模特:

export class RegistrationFormModel {
  //
  //
    Phones: Array<{Phone: string;}>;
  //
  //
}

And representation of this part of form 并表示这部分形式

     <ion-item *ngFor="let Phone of regModel.Phones; let i = index">

        <ion-label floating>Phone number*</ion-label>
        <ion-input type="tel" required [(ngModel)]="regModel.Phones[i].Phone" name="Phone" #Phone="ngModel"
                   pattern="\d{10}"></ion-input>
        <ion-icon *ngIf="i==0" name="ios-add-circle-outline" item-right no-padding
                  (click)="addPhone()"></ion-icon>
        <ion-icon *ngIf="i!=0" name="ios-remove-circle-outline" item-right no-padding
                  (click)="removePhone(i)"></ion-icon>

     </ion-item>

My methods for adding and removing phones. 我添加和删除手机的方法。 I added logs and incremental index for debug purposes: 我为调试目的添加了日志和增量索引:

debugIndex = 0;
 \\
 \\
  addPhone() {
    console.log('phones before add: ' + JSON.stringify(this.regModel.Phones));
    this.regModel.Phones.splice((this.regModel.Phones.length), 0, {Phone: '' + this.debugIndex++});
    console.log('phones after add: ' + JSON.stringify(this.regModel.Phones));
  }

  removePhone(i: number) {
    console.log('phones before delete: ' + JSON.stringify(this.regModel.Phones));
    this.regModel.Phones.splice(i, 1);
    console.log('phones after delete: ' + JSON.stringify(this.regModel.Phones));
  }

And from this part strange things happen. 从这一部分发生奇怪的事情。 According to logs data writes in my model properly but in UI last element replaces everything in input fields. 根据日志数据在我的模型中正确写入但在UI中最后一个元素替换输入字段中的所有内容。 But after removing one of the phones displayed phones for this moment seems like represent last state of UI. 但是在移除其中一部手机之后,此时显示的手机似乎代表了UI的最后状态。

在此输入图像描述

My logs captured during recording: 记录期间捕获的日志:

 "phones before add: [{"Phone":"123456789"}]", 
  "phones after add: [{"Phone":"123456789"},{"Phone":"0"}]", 
  "phones before add: [{"Phone":"123456789"},{"Phone":"4567890"}]", 
  "phones after add: [{"Phone":"123456789"},{"Phone":"4567890"},{"Phone":"1"}]", 
  "phones before delete: [{"Phone":"123456789"},{"Phone":"4567890"},{"Phone":"1"}]", 
  "phones after delete: [{"Phone":"123456789"},{"Phone":"4567890"}]", 
  "phones before add: [{"Phone":"123456789"},{"Phone":"4567890"}]", 
  "phones after add: [{"Phone":"123456789"},{"Phone":"4567890"},{"Phone":"2"}]", 
  "phones before add: [{"Phone":"123456"},{"Phone":"4567890"},{"Phone":"2"}]", 
  "phones after add: [{"Phone":"123456"},{"Phone":"4567890"},{"Phone":"2"},{"Phone":"3"}]", 
  "phones before add: [{"Phone":"123456"},{"Phone":"456789"},{"Phone":"2"},{"Phone":"3"}]", 
  "phones after add: [{"Phone":"123456"},{"Phone":"456789"},{"Phone":"2"},{"Phone":"3"},{"Phone":"4"}]", 
  "phones before delete: [{"Phone":"123456"},{"Phone":"456789"},{"Phone":"2"},{"Phone":"3"},{"Phone":"4"}]"
  "phones after delete: [{"Phone":"123456"},{"Phone":"456789"},{"Phone":"2"},{"Phone":"4"}]", 
  "phones before add: [{"Phone":"123456"},{"Phone":"456789"},{"Phone":"47890"},{"Phone":"4"}]", 
  "phones after add: [{"Phone":"123456"},{"Phone":"456789"},{"Phone":"47890"},{"Phone":"4"},{"Phone":"5"}]"

Any help appreciated and thanks in advance. 任何帮助表示赞赏和感谢。

Adding [ngModelOptions]="{standalone: true}" to your input should fix the problem: 在输入中添加[ngModelOptions]="{standalone: true}"可以解决问题:

<ion-input type="tel" required [(ngModel)]="regModel.Phones[i].Phone" 
[ngModelOptions]="{standalone: true} #Phone="ngModel" pattern="\d{10}">
</ion-input>

For every input with NgModel directive, FormControl will be created and it will be added to FormGroup , but when you add standalone: true , the fields won't be added to the FormGroup and this problem should be fixed. 对于具有NgModel指令的每个输入,将创建FormControl并将其添加到FormGroup ,但是当您添加standalone: true ,这些字段将不会添加到FormGroup并且应该修复此问题。 You should also remove name attribute from your input because only one of those is needed when using template driven forms. 您还应该从输入中删除name属性,因为在使用模板驱动表单时只需要其中一个属性。 ( name or standalone: true ) namestandalone: true

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

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