angular.js中一組復選框,怎么實現(xiàn)選中其中一個,其他不能選?
光陰似箭催人老,日月如移越少年。
ng-model
需要一致。用value控制內容.
<script>
angular.module('radioExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.color = {
name: 'blue'
};
$scope.specialValue = {
"id": "12345",
"value": "green"
};
}]);
</script>
<form name="myForm" ng-controller="ExampleController">
<label>
<input type="radio" ng-model="color.name" value="red">
Red
</label><br/>
<label>
<input type="radio" ng-model="color.name" ng-value="specialValue">
Green
</label><br/>
<label>
<input type="radio" ng-model="color.name" value="blue">
Blue
</label><br/>
<tt>color = {{color.name | json}}</tt><br/>
</form>
Note that `ng-value="specialValue"` sets radio item's value to be the value of `$scope.specialValue`.
以下代碼就是個思路,有很多問題,比如我發(fā)現(xiàn)的,列表不能抽取出來,用ng-for不可行。.disabled需要自己去調節(jié)樣式。
<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet">
</head>
<body>
<p ng-controller="demo">
<p>當前值: {{hobby}}</p>
<ul>
<li>
<label>吃:</label>
<input type="checkbox" ng-true-value="1" ng-model="hobby" ng-class="{disabled: hobby}" ng-change="handleCheckbox($event)" />
</li>
<li>
<label>睡:</label>
<input type="checkbox" ng-true-value="2" ng-model="hobby" ng-class="{disabled: hobby}" ng-change="handleCheckbox($event)" />
</li>
</ul>
</p>
<script src="http://cdn.bootcss.com/angular.js/1.5.7/angular.min.js"></script>
<script>
angular.module('app', [])
.controller('demo', ['$scope', function ($scope) {
$scope.hobby = false;
}]);
</script>
</body>
</html>