简体   繁体   English

Angular 2:用于布尔类型的无线电输入的ngModel绑定

[英]Angular 2: ngModel binding for radio input of boolean type

In Angular 2, I'd like to bind 2 radio button input options in 1 group to a model property of boolean type, however either I'm not able to select one of the radio buttons or running into another incorrect binding issue. 在Angular 2中,我想将1组中的2个单选按钮输入选项绑定到布尔类型的模型属性,但是我要么无法选择其中一个单选按钮,要么遇到另一个错误的绑定问题。 I've tried the following in my html. 我在我的HTML中尝试过以下内容。

*.component.html : *.component.html

Error: myModel.modelProperty is always: false, no matter which radio button is selected.

<div class="form-group">
        <label for="modelProperty">Model Property: </label>
        <form action="">
            <input type="radio" [ngModel]="_model.modelProperty"  (click)="_model.modelProperty=true" name="modelProperty" value=true>Yes<br>
            <input type="radio" [ngModel]="_model.modelProperty"  (click)="_model.modelProperty=false"  name="modelProperty" value=false>No
         </form>
</div> 

<div>{{_model.modelProperty}}</div>

*.component.html : *.component.html

    Error: myModel.modelProperty is [Object object], only No radio button can be selected, if either Yes or No radio buttons is clicked.

<div class="form-group">
            <label for="modelProperty">Model Property: </label>
            <form action="">
                <input type="radio" [(ngModel)]="_model.modelProperty"   name="modelProperty" ngValue=true>Yes<br>
                <input type="radio" [(ngModel)]="_model.modelProperty"   name="modelProperty" ngValue=false>No
             </form>
    </div> 

    <div>{{_model.modelProperty}}</div>

I'm using the following 我正在使用以下内容

*.component.ts for all *.component.html options above: *.component.ts适用于以上所有*.component.html选项:

import {Component, Input} from 'angular2/core';
import {NgForm}    from 'angular2/common';
import {Model}    from './model';
@Component({
  selector: 'my-form',
  templateUrl: 'app/.../*.component.html'
})

export class *Component {

      _model = new Model(..., false, ...); //false is the Model property: .modelProperty

      constructor(){}

      ...
}

要使您的html值作为布尔值计算,请使用: [value]="true"

In similar cases I use your first version of code with [checked] instead of [ngModel] . 在类似的情况下,我使用[checked]代替[ngModel]第一个代码版本。

This code works for me: 这段代码适合我:

<form action="">
    <input type="radio" [checked]="_model.modelProperty" 
        (click)="setProperty($event.target.checked)"
        name="modelProperty">Yes<br>
    <input type="radio" [checked]="!_model.modelProperty" 
        (click)="setProperty(!$event.target.checked)" 
        name="modelProperty">No 
</form> 

setProperty(inChecked: boolean) { 
    this._model.modelProperty = inChecked; 
}

For boolean, [(ngModel)] is working with [value] . 对于布尔值, [(ngModel)]正在使用[value] [(ngModel)] fails to checked by default with value . [(ngModel)]默认情况下无法使用value进行检查。 For eg: 例如:

<input type="radio" 
   id="idYes" 
   name="Preferred-group" 
   [value]="true" 
   [(ngModel)]="IsContactPreffered"> 

works fine. 工作良好。

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

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