【问题标题】:Why is ionic 3 two way data binding not working?为什么 ionic 3 双向数据绑定不起作用?
【发布时间】:2018-08-14 20:19:50
【问题描述】:

请我尝试为我正在进行的 ionic 项目充实一个注册页面 试图将我的 ([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>

即使采用上述设置,它也完全不起作用。 我可以在这里做什么来使两个数据绑定工作

【问题讨论】:

  • 控制台中没有错误信息?
  • 没有。它正确呈现。只是不绑定

标签: angular data-binding binding ionic3 ngmodel


【解决方案1】:

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

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.. 这意味着您正在使用数组索引引用这些对象。 ?
  • 不是数组索引,而是属性名。我添加了一个指向 this MDN documentation page 的链接,您可以在其中找到有关属性访问器的更多详细信息。
【解决方案2】:

这显示为一个单独的答案,因为我无法对 Connorsfan 的答案添加评论。

Connorsfan 的回答对我最初的问题非常有帮助,并为我指明了 Stackblitz 的方向,谢谢!

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

在 ConnorsFan 的 stackblitz 上构建,以添加具有多级支持的动态数据绑定,因为在使用表达式处理多级 json 时,banana-in-the-box [(ngModel)] 不起作用。可以在这个链接找到:https://stackblitz.com/edit/angular-ymvaml

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

【讨论】:

  • 请参考How do I write a good answer? 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效
猜你喜欢
  • 1970-01-01
  • 2010-10-07
  • 2016-08-20
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-26
相关资源
最近更新 更多