首页 > 代码库 > angular 复选框回选

angular 复选框回选

<!DOCTYPE html>
<html ng-app="myApp">
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript" src="http://www.mamicode.com/js/angular.min.js"></script>
	</head>
	<body ng-controller="main">
		<div ng-repeat="item in list">
			<input type="checkbox" ng-checked="isSelect(item.id)" value="http://www.mamicode.com/item.id" ng-click="select(item.id,$event)"/>{{item.device}}	
		</div>
		<div>{{result}}</div>
		<script>
			var app = angular.module(‘myApp‘,[]);
			app.controller(‘main‘,[‘$scope‘,function($scope) {
				$scope.list = [
				{‘id‘:1,‘device‘:‘摄影‘},
				{‘id‘:2,‘device‘:‘视屏‘},
				{‘id‘:3,‘device‘:‘音乐‘}											
				]
				$scope.result = [1];//默认选中1
				$scope.select = function(id,event) {
					console.log(event);
				
					var action = event.target;
						console.log(action);
					if(action.checked) {
						//选中添加
						if($scope.result.indexOf(id) == -1) {
							$scope.result.push(id);
						}
						
					}else {
						//不选中删除
						var selectId = $scope.result.indexOf(id);
						if(selectId !=-1) {
							$scope.result.splice(selectId,1);
						}
						
					}
					
				}
				$scope.isSelect = function(id) {
					return $scope.result.indexOf(id) != -1;
				}
			}]);
		</script>
	</body>
</html>

  

angular 复选框回选