AngularJS:反向复选框状态 [英] AngularJS: Reverse Checkbox State

查看:72
本文介绍了AngularJS:反向复选框状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以某种方式,通过Angular的魔力,如果您使用ng-model并将其提供为布尔值,则您会选中该布尔值是否为true的复选框,如果为false则取消选中该复选框.

Somehow, through the magic of Angular, if you use ng-model and provide it a boolean, your checkbox will be checked if said boolean is true, and unchecked if false.

<input type="checkbox" ng-model="video.hidden">

虽然这很令人困惑,但我实际上是在尝试反转选中状态,因为与待办事项示例不同,其中todo.done表示复选框已选中,我的模型更像是todo.incomplete.

While this alone is fairly baffling, I'm actually trying to reverse the checked state, because unlike the todo example where todo.done means the box gets checked, my model is more like todo.incomplete.

不幸的是,我的第一个猜测没有奏效:

Unfortunately my first guess didn't work:

<input type="checkbox" ng-model="!video.hidden">

我所处的位置已经决定了模型,因此我无法更改它,也不必在客户端上进行按摩(因为我正在将客户端对象发送回服务器) ,因为它在受信任的环境中运行).

I'm in a position where the model has been dictated to me, so I can't change it and don't want to have to massage it on the client (because I'm sending client objects back to the server, since it's running in a trusted environment).

更新

这在1.3中有效,并且不会给您字符串(1.2.xxx为您提供字符串而不是布尔值):

This works in 1.3 and doesn't give you strings (1.2.xxx gave you strings instead of booleans):

<input type="checkbox" ng-model="video.hidden" ng-true-value="false" ng-false-value="true">

推荐答案

您现在可以不需要自定义指令,角度支持ng-true-valueng-false-value

You can make that now without a need of custom directive, angular support ng-true-value and ng-false-value

https://docs.angularjs.org/api/ng/input/input [复选框]

您的示例现在应该可以工作 <input type="checkbox" ng-model="video.hidden" ng-true-value="false" ng-false-value="true">

your example should work now <input type="checkbox" ng-model="video.hidden" ng-true-value="false" ng-false-value="true">

这篇关于AngularJS:反向复选框状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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