angular.js - How to use angular to make the checkbox single-selected so that only one can be selected?
習慣沉默
習慣沉默 2017-05-15 17:03:18
0
2
624

How to use angular to make the checkbox single-selected so that only one can be selected?

As shown in the picture, the following are the checkbox options in the table:

<td width="30" style="text-align: left"><input
                            type="checkbox" ng-model="checkboxes.items[item.bookingId]" name="flag" ng-click="updateSelection(item.bookingId,checkboxes.items)"/></td>

If it is implemented in the object's controller, it can only be selected single-selected. What about selecting one? ?

習慣沉默
習慣沉默

reply all(2)
迷茫

checkbox is a check box, please use radio for single selection

Ty80

Try this

<p ng-controller="MyCtrl">
    <p ng-repeat="item in list">
        <label><input type="checkbox" ng-model="item.checked" ng-change="onChange(item)">{{item.name}}</label>
    </p>
</p>
.controller('MyCtrl', function ($scope) {
    $scope.selected = '';
    $scope.list = [
        {id: 1, name: 'xxx'},
        {id: 2, name: 'yyy'},
        {id: 3, name: 'zzz'}
    ];

    $scope.onChange = function (item) {
        if (item.checked) {
            if (!$scope.selected) $scope.selected = item;
            if ($scope.selected !== item) item.checked = false;
        } else {
            $scope.selected = '';
        }
    }
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template