如何在离子无线电元件上使用 ngModel?

How can I use a ngModel on an ion-radio element?(如何在离子无线电元件上使用 ngModel?)
本文介绍了如何在离子无线电元件上使用 ngModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 ion-radio 元素上实现 ngModel,但不知何故它不起作用.这是我的代码:

I'm trying to implement a ngModel on a ion-radio element but somehow it doesn't work. This is my code:

import {
  Page
} from 'ionic-angular';

@Page({
  templateUrl: 'build/pages/settings/settings.html'
})

export class Settings {
  constructor() {
    this.unit = 2;
  }
}

<ion-list radio-group>
  <ion-list-header>
    Unit
  </ion-list-header>

  <ion-item>
    <ion-label>Metric (kg)</ion-label>
    <ion-radio value="1" [(ngModel)]="unit"></ion-radio>
  </ion-item>

  <ion-item>
    <ion-label>Imperial (lbs)</ion-label>
    <ion-radio value="2" [(ngModel)]="unit"></ion-radio>
  </ion-item>
</ion-list>

我尝试在离子输入和离子选择上实现它,效果很好.我还尝试将 directives: [FORM_DIRECTIVES] 添加到我的 @Page 并添加相应的导入,但这并不能解决问题.

I've tried implementing it on a ion-input and ion-select and that just works fine. I also tried adding directives: [FORM_DIRECTIVES] to my @Page and added the corresponding import but that doesn't fix the problem.

有什么想法吗?

推荐答案

语法已经改写,ngModel 应该放在 ion-list &radio-group 只有一次.无需将它们放在每个 ion-radio 元素上.

Syntax has been changed rewritten now, ngModel should be place with ion-list & radio-group only once. No need to have them there on each ion-radio element.

<ion-list radio-group [(ngModel)]="unit">
    <ion-list-header>
        Unit
    </ion-list-header>

    <ion-item>
        <ion-label>Metric (kg)</ion-label>
        <ion-radio value="1"></ion-radio>
    </ion-item>

    <ion-item>
        <ion-label>Imperial (lbs)</ion-label>
        <ion-radio value="2" ></ion-radio>
    </ion-item>
</ion-list>

有关更多信息,您可以访问 ionic2 框架论坛链接

For more information you could visit ionic2 framework forum link

这篇关于如何在离子无线电元件上使用 ngModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)