陈荣凯
陈荣凯

Reputation: 13

AngularJS checkbox model does not update when checked

When I use Angular 1.6, a checkbox is checked on page load, but when I click the checkbox the checkbox model is not update. Why?

Code:

  var app = angular.module("demo",[]);
  app.controller("ctrl",["$scope",function($scope){
    var data = {
      C_DJB:{VAL:'1'}
    }
      $scope.obj = data;

  }])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <meta name="description" content="test">
        <meta name="author" content="">
        <title>Page Title</title>

    </head>
    <body ng-app="demo" ng-controller="ctrl">
        <h1>Body</h1>
        <input  type="checkbox" ng-model="obj.C_DJB.VAL" ng-true-value="1" ng-false-value="0" ng-checked="obj.C_DJB.VAL=='1'" /> <br/>
        <input type="checkbox" ng-model="obj.C_DJB.VAL" ng-true-value="1" ng-false-value="0" ng-checked="obj.C_DJB.VAL=='1'" /> <br/>
        {{obj.C_DJB.VAL}}
 

    </body>
</html>

Upvotes: 1

Views: 1985

Answers (4)

onmyway
onmyway

Reputation: 1505

  var app = angular.module("demo",[]);
  app.controller("ctrl",["$scope",function($scope){
    var data = {
      C_DJB:{VAL:'0'}
    }
      $scope.obj = data;

  }])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <meta name="description" content="test">
        <meta name="author" content="">
        <title>Page Title</title>

    </head>
    <body ng-app="demo" ng-controller="ctrl">
        <h1>Body</h1>
        <input  type="checkbox" ng-model="obj.C_DJB.VAL" ng-true-value="1" ng-false-value="0"" /> <br/>
        <input type="checkbox" ng-model="obj.C_DJB.VAL" ng-true-value="1" ng-false-value="0"" /> <br/>
        {{obj.C_DJB.VAL}}
 

    </body>
</html>

Upvotes: 0

陈荣凯
陈荣凯

Reputation: 13

var app = angular.module("demo",[]);
  app.controller("ctrl",["$scope",function($scope){
    var data = {
      C_DJB:{VAL:'1'}
    }
      $scope.obj = data;

  }])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <meta name="description" content="test">
        <meta name="author" content="">
        <title>Page Title</title>

    </head>
    <body ng-app="demo" ng-controller="ctrl">
        <h1>Body</h1>
        <input  type="checkbox" ng-model="obj.C_DJB.VAL" ng-true-value="1" ng-false-value="0" ng-checked="obj.C_DJB.VAL=='1'" /> <br/>
        <input type="checkbox" ng-model="obj.C_DJB.VAL" ng-true-value="1" ng-false-value="0" ng-checked="obj.C_DJB.VAL=='1'" /> <br/>
        {{obj.C_DJB.VAL}}
 

    </body>
</html>

Upvotes: 0

AKSHAY JAIN
AKSHAY JAIN

Reputation: 246

You need to remove

ng-checked="obj.C_DJB.VAL=='1'"

As you have defined in the controller

 var data = {
  C_DJB:{VAL:'1'}
}

so by default the checkbox will be checked. Now when you check or uncheck the checkbox the value will get updated accordingly. The problem is when you are giving ng-checked="obj.C_DJB.VAL=='1'" the checbox value always remains 1 whether you tick or untick the checkbox.

Upvotes: 1

Ibrahim Maher
Ibrahim Maher

Reputation: 103

The value being printed is changing from 1 to 0 when you untick the checkbox, which means the model is getting updated just fine.

Upvotes: 1

Related Questions