Angular2 - 单选按钮绑定 [英] Angular2 - Radio Button Binding

查看:28
本文介绍了Angular2 - 单选按钮绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use radio button in a form using 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

model.options initial value is 1

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

when the page is loaded the first radio button isn't checked and the modifications aren't binded to the model

有什么想法吗?

推荐答案

使用 [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 所建议的对于 angular 2.1+ 使用 [(ngModel)] 而不是 [(ng-model)]"

As suggested by thllbrg "For angular 2.1+ use [(ngModel)] instead of [(ng-model)] "

这篇关于Angular2 - 单选按钮绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆