为了账号安全,请及时绑定邮箱和手机立即绑定

Angular2-单选按钮绑定

Angular2-单选按钮绑定

浮云间 2019-10-28 15:35:12
我想在使用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加载页面时,未选中第一个单选按钮,并且修改未绑定到模型任何想法 ?
查看完整描述

3 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

使用[value] =“ 1”代替value =“ 1”


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


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

编辑:


如所建议的通过thllbrg“对于角度2.1+使用[(ngModel)],而不是[(ng-model)]”


查看完整回答
反对 回复 2019-10-28
?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

我的手动解决方法model.options是在选择新的单选按钮时手动进行更新:


template: `

  <label *ngFor="let item of radioItems">

    <input type="radio" name="options" (click)="model.options = item" 

     [checked]="item === model.options">

    {{item}}

  </label>`


class App {

  radioItems = 'one two three'.split(' ');

  model      = { options: 'two' };

}

这Plunker演示了上面的内容,以及如何使用按钮来更改所选的单选按钮-即证明数据绑定是双向的:


<button (click)="model.options = 'one'">set one</button>


查看完整回答
反对 回复 2019-10-28
  • 3 回答
  • 0 关注
  • 1151 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信