在Angular 2中,我想將1組中的2個單選按鈕輸入選項綁定到布爾類型的模型屬性,但是無論我選擇哪一個的單選按鈕或運行到另一個不正確的綁定問題。 我試過以下在我的HTML。Angular 2:ngModel爲布爾類型的無線電輸入綁定
*.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(){}
...
}
模型屬性值是正確的,具體取決於它們選擇的單選按鈕,但從視覺上看,單擊時單選按鈕並不總是被選中。 – jerryh91
我最近的評論有任何更新? – jerryh91
此代碼適用於我
'和' setProperty(inChecked:boolean){this._model.modelProperty = inChecked; }' – Picci