Matthew Layton
Matthew Layton

Reputation: 42390

angular checkbox does not bind to model

In my controller I have this member:

$scope.sameOptionsOnReturn = true;

and in my view:

<input type="checkbox"
       ng-model="sameOptionsOnReturn"
       ng-checked="sameOptionsOnReturn"
       ng-value="true"
       ng-change="setReturnOptions" />

But the input does not bind to the checkbox; it's always true. What is wrong?

Note: removing ng-value="true" doesn't make any difference.

Upvotes: 2

Views: 489

Answers (2)

gyc
gyc

Reputation: 4360

Since it does work in the snippet below, I have to assume there's something wrong elsewhere in your code.

function SuperController($scope) {
	$scope.sameOptionsOnReturn = true;
}

angular.module('myApp', []);
angular
    .module('myApp')
    .controller('SuperController', SuperController)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="SuperController as s">
    <input type="checkbox"
       ng-model="sameOptionsOnReturn"
       ng-checked="sameOptionsOnReturn"
       ng-value="true"
       ng-change="setReturnOptions" />
    
    {{sameOptionsOnReturn}}
  </div>
</div>

Upvotes: 2

Sumit Chaudhari
Sumit Chaudhari

Reputation: 210

ng-model works for your bindings . So you don`t need to put ng-value="true" which making your checkbox always checked .remove it from the input checkbox . ng-model value will make your checkbox checked or unchecked .

Upvotes: 0

Related Questions