angularjs - 使用 AngularJS : Bind a radio button input to a Boolean with possible null value

标签 angularjs

使用 AngularJS v1.5.0,我尝试将 radio 输入字段绑定(bind)到 bool 变量,该变量可以是:true、false 或 null。使用 ng-value 指令将 null 值设置为 false。

<input type="radio" ng-model="foo.value" ng-value="true"  ng-change="update()">
<input type="radio" ng-model="foo.value" ng-value="false" ng-change="update()">
<input type="radio" ng-model="foo.value" ng-value="null"  ng-change="update()"> 

您知道如何做到这一点吗? 谢谢!

最佳答案

这在 1.4.9 中对我有用。 1.5.0和1.6.3

<span ng-init="foo = {value: null}">{{foo}}</span>
<input type="radio" ng-model="foo.value" ng-value="null" ng-checked="foo.value === null" />Null
<input type="radio" ng-model="foo.value" ng-value="true" />True
<input type="radio" ng-model="foo.value" ng-value="false" />False

JsFiddle

关于angularjs - 使用 AngularJS : Bind a radio button input to a Boolean with possible null value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35385183/

相关文章:

php - Angular API 日期不工作 : timestamp from PHP backend needs to be refactored?

javascript - 如何使用 $q.defer 链接 ajax 请求?

javascript - 使用 NodeJS 将开始和停止时间发布到 mysql(sequelize)

javascript - Angular - 从 Controller 访问动态创建的表单元素

javascript - ionic2 从函数检索数据到 html 页面

javascript - IE浏览器日期时间问题

javascript - 如何将 Bootstrap 模式窗口与 angularjs 一起使用?

javascript - ng-repeat 返回类型错误 : Cannot read property 'insertBefore' of null

javascript - put 请求如何通过 Angular、Express 和 Mongoose 工作?

javascript - 在 AngularJs 中从 url 中删除 # 时出现问题