gpt4 book ai didi

typescript - Angular 2 : ngModel binding for radio input of boolean type

转载 作者:太空狗 更新时间:2023-10-29 17:17:14 27 4
gpt4 key购买 nike

在 Angular 2 中,我想将 1 组中的 2 个单选按钮输入选项绑定(bind)到 bool 类型的模型属性,但是要么我无法选择其中一个单选按钮,要么遇到另一个不正确的绑定(bind)问题.我在我的 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:

    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>

我正在使用以下内容

*.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 值评估为 bool 值,请使用:[value]="true"

关于typescript - Angular 2 : ngModel binding for radio input of boolean type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36802522/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com