Trong Góc 2, tôi muốn kết hợp 2 tùy chọn đầu vào nút radio trong 1 nhóm thành thuộc tính mô hình của kiểu boolean, tuy nhiên tôi không thể chọn các nút radio hoặc chạy vào một vấn đề ràng buộc không chính xác khác. Tôi đã thử những điều sau đây trong html của tôi.Góc 2: ngModel ràng buộc cho đầu vào radio của kiểu boolean
*.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>
Tôi đang sử dụng sau
*.component.ts
cho tất cả *.component.html
tùy chọn ở trên:
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(){}
...
}
giá trị thuộc tính mô hình là chính xác, tùy thuộc vào nút radio họ chọn, tuy nhiên trực quan, các nút radio không nhất quán được chọn khi được nhấp. – jerryh91
Mọi cập nhật về nhận xét cuối cùng của tôi? – jerryh91
Mã này hoạt động cho tôi '
' với ' setProperty (inChecked: boolean) { this._model.modelProperty = inChecked; } ' – Picci