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