为什么离子3双向数据绑定不起作用?

时间:2018-03-06 21:04:48

标签: angular data-binding binding ionic3 ngmodel

请我试着充实我的离子正在进行的项目的注册页面 尝试将我的([ngModel])绑定到组件中的对象属性。

让我只显示代码摘录,以便您理解

**Registration.ts** // This is my registration component
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

@IonicPage()
@Component({
  selector: 'page-registration',
  templateUrl: 'registration.html',
})

export class RegistrationPage {
  newStaffInfo = {
      username: "",
      password: "",
      rePassword: "",
      email: "",
      sex: ""
  }


newStaffTemplateObject: Object = [
      {
        label: "Username",
        field: this.newStaffInfo.username,
      },

      {
        label: "Password",
        field: this.newStaffInfo.password  
      },

      {
        label: "Re-enter Your password",
        field: this.newStaffInfo.rePassword  
      },
      {
        label: "Email",
        field: this.newStaffInfo.email  
      },

      {
        label: "Sex",
        field: this.newStaffInfo.sex  
      },

  ];

  constructor(public navCtrl: NavController, public navParams: NavParams) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad RegistrationPage');
  }


  validateForm(){
      console.log("Validate in this functin...");
  }
}

这是我的HTML模板

<ion-content padding>

    <form (ngSubmit)="validateForm()" #form="ngForm">
        <ion-list>

            <ion-item *ngFor="let item of newStaffTemplateObject">
                <ion-label floating> {{item.label}} </ion-label>
                <ion-input type="text" [(ngModel)]="item.field" name="item.label" #item.label="ngModel" required> </ion-input>
            </ion-item>

            <ion-item>
                <button ion-button outline block full> Register </button>
            </ion-item>

        </ion-list>
    </form>

<!-- For DEbugging puprpose only -->

{{newStaffInfo.username}}  //This is suppose to reflect changes immediately
</ion-content>

即使使用上述设置,它根本不起作用。 我能做些什么来使两个数据绑定工作

2 个答案:

答案 0 :(得分:2)

在您的代码中,您使用newStaffTemplateObject的属性初始化newStaffInfo项:

newStaffTemplateObject = [
  {
    label: "Username",
    field: this.newStaffInfo.username
  },
  {
    label: "Password",
    field: this.newStaffInfo.password
  },
  ...
];

然后将newStaffTemplateObject项绑定到模板中的输入元素。

数据绑定有效:它更新newStaffTemplateObject项的值。但是,它不会更新newStaffInfo中的相关属性(根据您的调试标记,这是您的预期)。原因是:newStaffTemplateObject[0].field不会成为对newStaffInfo.username的引用;它是一个单独的变量,在初始化后不与它保持任何链接。

一种可能的解决方案是将每个field值设置为newStaffInfo中的属性名称:

newStaffTemplateObject = [
  {
    label: "Username",
    field: "username",
  },
  {
    label: "Password",
    field: "password"
  },
  {
    label: "Re-enter password",
    field: "rePassword"
  },
  {
    label: "Email",
    field: "email"
  },
  {
    label: "Sex",
    field: "sex"
  },
];

并使用bracket notationnewStaffInfo的属性绑定到输入元素:

<ion-item *ngFor="let item of newStaffTemplateObject">
  ...
  <ion-input type="text" [(ngModel)]="newStaffInfo[item.field]" ... > </ion-input>
</ion-item>

您可以在this stackblitz中测试代码。

答案 1 :(得分:1)

由于我无法在Connorsfan的答案中添加评论,因此这显示为单独的答案。

Connorsfan的回答对我最初的问题很有帮助,并向我指出了Stackblitz的方向,谢谢!

我还遇到了一个有趣的问题,试图浏览多层json并使用真正的动态表单。

建立在ConnorsFan的stackblitz上,以添加具有多级支持的动态数据绑定,因为当使用表达式处理多级json时,香蕉盒中的[[ngModel)]不起作用。可以在以下链接中找到:https://stackblitz.com/edit/angular-ymvaml

希望这将帮助其他面临相同问题但使用多层json的人。