【问题标题】:Using ngFor with ngModel dynamic data wrong behaviour使用 ngFor 和 ngModel 动态数据错误行为
【发布时间】:2016-12-21 15:00:34
【问题描述】:

我的应用程序逻辑中存在复杂且可能是新手的错误,因此我将尝试提供全面的信息。

我已将注册表单绑定到我的数据模型。电话号码字段可以由用户在注册时添加并保存为数组。

我的模特:

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

以及这部分表格的表示

     <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>

我添加和删除手机的方法。我为调试目的添加了日志和增量索引:

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));
  }

从这部分奇怪的事情发生了。根据日志,数据正确写入我的模型,但在 UI 中,最后一个元素替换了输入字段中的所有内容。 但在移除其中一部手机后,此时显示的手机似乎代表了 UI 的最后状态。

我在录制过程中捕获的日志:

 "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"}]"

任何帮助表示赞赏并提前致谢。

【问题讨论】:

    标签: javascript angular ionic-framework angular2-forms


    【解决方案1】:

    在您的输入中添加[ngModelOptions]="{standalone: true}" 应该可以解决问题:

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

    对于带有NgModel 指令的每个输入,将创建FormControl 并将其添加到FormGroup,但是当您添加standalone: true 时,字段将不会添加到FormGroup 中问题应该得到解决。您还应该从输入中删除 name 属性,因为在使用模板驱动的表单时只需要其中一个。 (namestandalone: true

    【讨论】:

    • 如果您通过添加“standalone: true”从 FormGroup 中删除字段。你如何使用内置的表单验证?
    猜你喜欢
    • 2020-11-06
    • 1970-01-01
    • 2016-10-11
    • 2018-02-24
    • 2018-11-23
    • 2016-11-07
    • 2021-09-12
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多