1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ngCheckedの変更がngModelにバインドされない

Last updated at Posted at 2014-01-15

ngCheckedを使ってチェックボックスの連動をさせると、表示上はチェックが入るがngModelにはtrue/falseが反映されない。
反映元のngChangeを拾ってJavaScript側で書けば回避できるが、なんか釈然としない。(そもそもngCheckedはそういう風に使うもんではないのかもちれんが)

親しか変更が反映されない
<!DOCTYPE html>
<html lang="ja" ng-app="testApp">

<head>
<meta charset="utf-8">
<title>ngModelとngCheckedの動作チェック</title>
</head>

<body>
<div ng-controller="testCtrl">
	<form novalidate>
		<input type="checkbox" ng-model="checkbox.parent"><br>
		<input type="checkbox" ng-model="checkbox.child" ng-checked="checkbox.parent"><br>
		{{checkbox|json}}<br>
	</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<script>
var testApp = angular.module('testApp', []);
testApp.controller('testCtrl', function ($scope) {
	return false;
});
</script>
</body>

</html>
scriptを追記すれば反映される
var testApp = angular.module('testApp', []);
testApp.controller('testCtrl', function ($scope) {
	$scope.setChildCheckbox = function(){
		$scope.checkbox.child = $scope.checkbox.parent;
		return false;
	};
	return false;
});
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?