这里说的是angularjs 1.x

在实现单选框时,我们完全可以用html自带的<input type="radio"/>,但是配合angularjs 中的双向绑定,我们可以很方便的实现选择不同的radio,立刻更新绑定的数据。

一般会这样做

<input type="radio" name="gender" ng-model="sex" value="false" />
<input type="radio" name="gender" ng-model="sex" value="true" />

这样做我们在控制器中如果有$scope.sex; 一般点选会将字符串 false或 true绑定到 sex变量上,但是。如果我们想把一个bool值或者对象绑定到一个变量上,上面的做法是不行的,因为value属性只能绑定字符串。要绑定对象和bool类型必须用angularjs 提供的ng-value指令。

官方api有一个 对input[radio]中ngValue的说明:

AngularJS expression to which ngModel will be be set when the radio is selected. Should be used instead of the value attribute if you need a non-string ngModel (booleanarray, ...).

就是说如果ng-model绑定一个费字符串类型的变量,请用ng-value属性替换 value属性。

如果一个页面中有多组单选框,请将同一组的单选框用同一个name属性值标示好。

<input type="radio" name="gender" ng-model="sex" ng-value="false" />
<input type="radio" name="gender" ng-model="sex" ng-value="true"checked="true /> <input type="radio" name="graduate" ng-model="graduate" ng-value="false" />
<input type="radio" name="graduate" ng-model="graduate" ng-value="true" />

如上,name="gender" 是表示性别的一组单选按钮,name="graduate"是表示是否毕业的一组单选按钮。

angularjs中的单选框绑定数据注意事项的更多相关文章

  1. angularJS中directive父子组件的数据交互

    angularJS中directive父子组件的数据交互 1. 使用共享 scope 的时候,可以直接从父 scope 中共享属性.使用隔离 scope 的时候,无法从父 scope 中共享属性.在 ...

  2. 后端接收不到AngularJs中$http.post发送的数据的问题

    1.问题: 后端接收不到AngularJs中$http.post发送的数据,总是显示为null 示例代码: $http.post(/admin/KeyValue/GetListByPage, { pa ...

  3. AngularJS中使用$http对MongoLab数据表进行增删改查

    本篇体验使用AngularJS中的$http对MongoLab数据表进行增删改查. 主页面: <button ng-click="loadCourse()">Load ...

  4. 单击行,自己主动选中当前行中的单选框button

    需求:单击行,自己主动选中当前行中的单选框button. aspx页面: <asp:Repeater ID="rptRecordList" runat="serve ...

  5. easy ui 下拉框绑定数据select控件

    easy ui 中的下拉框控件叫做select,具体代码如下: html代码:①.这是一个公司等级的下拉框 <tr> <td>公司等级:</td> <td&g ...

  6. knockoutjs中使用mapping插件绑定数据列表

    使用KO绑定数据列表示例:   1.先申请V,T,T2三个辅助方法,方便调试.声明viewModel和加载数据时的映射条件mapping    2.先使用ko.mapping.fromJS()将原来的 ...

  7. easy ui 下拉级联效果 ,下拉框绑定数据select控件

    html代码: ①两个下拉框,一个是省,另一个市 <tr> <td>省:</td> <td> <select id="ProvinceI ...

  8. angularjs中动态为audio绑定src

    今天在angularjs中用audio的时候碰到的这些问题,查阅http://www.cnblogs.com/rachelanlan/p/3598070.html获得解决,感谢! <div cl ...

  9. Silverlight中的TabControl如何绑定数据?重写tabcontrol和tabItem 解决绑定友好问题。可以绑定对象集合

    在 WPF 中,TabControl 可以直接将 ItemsSource 绑定数据源,见 将 TabControl 绑定到数据的示例 http://msdn.microsoft.com/zh-cn/l ...

随机推荐

  1. Dubbo -- 系统学习 笔记 -- 示例 -- 集群容错

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 集群容错 在集群调用失败时,Dubbo提供了多种容错方案,缺省为failover重 ...

  2. ios开发之--[_NSInlineData objectForKeyedSubscript:]

    reason: '-[_NSInlineData objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7fa2049 ...

  3. MySQL开发面试题

    ……继上一篇MySQL的开发总结之后,适当的练习还是很有必要的…… SQL语法多变,不敢保证唯一,也不敢保证全对,如果错误欢迎指出,即刻修改. 一.现有表结构如下图 TABLENAME:afinfo ...

  4. [Git] 解决 insufficient permission for adding an object to repository database

    [环境] OS: CentOS 6.5 Git: 1.7.1 [症状描述] Git 中心仓库路径 ~/project.git,克隆库路径 ~/project.clone,克隆库中包含一个文件 ~/pr ...

  5. java多线程例子(生成者和消费者)

    Info.cs 商品 public class Info { boolean flag=false; private String name="张三"; private int a ...

  6. Geeks面试题:Min Cost Path

    Min Cost Path   Given a cost matrix cost[][] and a position (m, n) in cost[][], write a function tha ...

  7. Underscore.js(JavaScript对象操作方法)

    Underscore封装了常用的JavaScript对象操作方法,用于提高开发效率.(Underscore还可以被使用在Node.js运行环境.) 在学习Underscore之前,你应该先保存它的AP ...

  8. springbatch---->springbatch的使用(三)

    这里我们对上篇博客的例子做一个修改性的测试来学习一下springbatch的一些关于chunk的一些有用的特性.我渐渐能意会到,深刻并不等于接近事实. springbatch的学习 一.chunk的s ...

  9. centos 安装 Vagrant

    使用的软件: 1. CentOS:  CentOS release 6.4 (Final) 2. Vagrant: vagrant_1.2.2_i686.rpm 3. Virtualbox: Virt ...

  10. python tkinter教程-事件绑定

    一个Tkinter主要跑在mainloop进程里.Events可能来自多个地方,比如按键,鼠标,或是系统事件. Tkinter提供了丰富的方法来处理这些事件.对于每一个控件Widget,你都可以为其绑 ...