search

Home  >  Q&A  >  body text

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

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? ?

習慣沉默習慣沉默2777 days ago619

reply all(2)I'll reply

  • 迷茫

    迷茫2017-05-15 17:05:18

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

    reply
    0
  • PHP中文网

    PHP中文网2017-05-15 17:05:18

    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 = '';
            }
        }
    });

    reply
    0
  • Cancelreply