Angular2-单选按钮绑定

我想在使用Angular 2的表单中使用单选按钮


Options : <br/>


1 : <input name="options" ng-control="options" type="radio" value="1"  [(ng-model)]="model.options" ><br/>


2 : <input name="options" ng-control="options" type="radio" value="2" [(ng-model)]="model.options" ><br/>

model.options初始值为1


加载页面时,未选中第一个单选按钮,并且修改未绑定到模型


任何想法 ?


浮云间
浏览 1162回答 3
3回答

手掌心

使用[value] =“ 1”代替value =“ 1”<input name="options" ng-control="options" type="radio" [value]="1"&nbsp; [(ngModel)]="model.options" ><br/><input name="options" ng-control="options" type="radio" [value]="2" [(ngModel)]="model.options" ><br/>编辑:如所建议的通过thllbrg“对于角度2.1+使用[(ngModel)],而不是[(ng-model)]”

慕尼黑5688855

我的手动解决方法model.options是在选择新的单选按钮时手动进行更新:template: `&nbsp; <label *ngFor="let item of radioItems">&nbsp; &nbsp; <input type="radio" name="options" (click)="model.options = item"&nbsp;&nbsp; &nbsp; &nbsp;[checked]="item === model.options">&nbsp; &nbsp; {{item}}&nbsp; </label>`class App {&nbsp; radioItems = 'one two three'.split(' ');&nbsp; model&nbsp; &nbsp; &nbsp; = { options: 'two' };}这Plunker演示了上面的内容,以及如何使用按钮来更改所选的单选按钮-即证明数据绑定是双向的:<button (click)="model.options = 'one'">set one</button>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

AngularJS