简体   繁体   English

AngularJS取消选中单选按钮

[英]AngularJS uncheck radio button

I have tried to look at other solutions for this with ng-dblclick, $event etc but none of them seems to works for me. 我试图用ng-dblclick,$ event等查看其他解决方案,但似乎没有一个适合我。

I have two radio buttons inside a ng-repeat in a table. 我在桌子的ng-repeat里面有两个单选按钮。 I have one field for setting which radio button should be active. 我有一个字段用于设置哪个单选按钮应处于活动状态。 This works fine with just ng-change but I want to be able to deselect any of the radio buttons aswell. 仅使用ng-change即可正常工作,但我也希望能够取消选择任何单选按钮。

Ng-change doesn't fire on this so I added one for ng-click aswell and the messy code under works but is it any cleaner way of doing this? Ng-change不会触发此操作,因此我也为ng-click添加了一个,并且正在运行的代码杂乱,但这是否是更干净的方法?

<td>
    <div class="inline-group">
        <label class="radio">
            <input type="radio" name="stage{{$index}}" ng-change="save(item, false)" ng-click="save(item, true)" ng-model="item.Stage" value="Started">
            <i></i> Started
        </label>
        <label class="radio">
            <input type="radio" name="stage{{$index}}" ng-change="save(item, false)" ng-click="save(item, true)" ng-model="item.Stage" value="Completed">
            <i></i> Completed
        </label>
    </div>
</td>

Controller 控制者

var runOnce = false;

$scope.save = function (item, uncheck) {
    if (runOnce) {
        return;
    }

    if (uncheck) {
        item.stage = null;
    }
    else {
        runOnce = true;
    }

    ...
    .$promise.then(function (result) {
        ...
        runOnce = false;
        ...
     });
    ...

Look at the documentation 查看文档

<div class="inline-group">
        <label class="radio">
            <input type="radio" name="stage{{$index}}" ng-model="item.Stage" value="true">
            <i></i> Started
        </label>
        <label class="radio">
            <input type="radio" name="stage{{$index}}" ng-model="item.Stage" value="false">
            <i></i> Completed
        </label>
    </div>

And for a true / false selection, use a check box instead of a radio buttton 对于正确/错误的选择,请使用复选框而不是无线电按钮

Updated 更新

With an enum, in your scope: 在您的范围内有一个枚举:

$scope.stageEnum = { Started: 0, Completed: 1 };

your view : 您的看法:

<div class="inline-group">
            <label class="radio">
                <input type="radio" name="stage{{$index}}" ng-model="item.Stage" value="stageEnum.Started">
                <i></i> Started
            </label>
            <label class="radio">
                <input type="radio" name="stage{{$index}}" ng-model="item.Stage" value="stageEnum.Completed">
                <i></i> Completed
            </label>
        </div>

Radio buttons can be selected only one at a time and cannot be unchecked by the user once they are checked (unless you do programmatically, as you have tried). 单选按钮一次只能选择一个,并且一旦被选中就不能被用户取消选中(除非您已经尝试过以编程方式进行操作)。 Perhaps, an example for a more cleaner approach would have been: 也许,更干净的方法的一个例子是:

<input type="radio" ng-model="checked" value="500" ng-click="uncheck($event)" />
<input type="radio" ng-model="checked" value="1000" ng-click="uncheck($event)" />
<input type="radio" ng-model="checked" value="1500" ng-click="uncheck($event)" />

In your controller: 在您的控制器中:

$scope.uncheck = function (event) {
    if ($scope.checked == event.target.value)
        $scope.checked = false
}

DEMO: http://jsfiddle.net/8s4m2e5e/3/ 演示: http : //jsfiddle.net/8s4m2e5e/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM