Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ng-checked not changing model value

Tags:

angularjs

I have a table of objects with checkbox inputs. If an object's checkbox is checked, the object.isChecked value is set to true, and if it's unchecked then the value is set to false. However, I have a master checkbox that checks/unchecks all the checkboxes in the table. This does not update the object.isChecked values however. How would I make the master checkbox change the object.isChecked values?

like image 961
Corbin Lewis Avatar asked Aug 13 '13 22:08

Corbin Lewis


People also ask

How to check if checkbox is checked or not in AngularJS?

The ng-checked Directive in AngularJS is used to read the checked or unchecked state of the checkbox or radio button to true or false. If the expression inside the ng-checked attribute returns true then the checkbox/radio button will be checked otherwise it will be unchecked.

How to uncheck the checkbox in AngularJS?

On the first checkbox click changes $scope. checkall value and update all other checkboxes user. checked=$scope=checkall . Check uncheck the first checkbox.

Does Ng-change require NG-model?

The ng-change directive requires a ng-model directive to be present. The ng-change directive from AngularJS will not override the element's original onchange event, both the ng-change expression and the original onchange event will be executed.

What is NG-model and Ng-change?

Ng-change is a directive in AngularJS which is meant for performing operations when a component value or event is changed. In other words, ng-change directive tells AngularJS what to do when the value of an HTML element changes. An ng-model directive is required by the ng-change directive.


1 Answers

The problem must be you trigger checkboxes not inside Angular. If you want Angular magic to work - you must do all your model manipulations inside Angular scope. I've created a plunker to demonstrate.:

index.html

<!DOCTYPE html>
<html data-ng-app="demo">
  <head>
    <script data-require="[email protected]" data-semver="1.9.0" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.0/jquery.js"></script>
    <script data-require="angular.js@*" data-semver="1.0.7" src="http://code.angularjs.org/1.0.7/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>
  <body data-ng-controller="DemoController">
    <div data-ng-repeat="object in objects">
      {{object.name}}: <input type="checkbox" data-ng-model="object.isChecked">
    </div>
    Master: <input type="checkbox" data-ng-click="triggerAll()">
    {{objects}}
  </body>
</html>

script.js

"use strict";

var demo = angular.module("demo", []);

function DemoController($scope) {
  $scope.objects = [
    {
      name : "First",
      isChecked : true
    },
    {
      name : "Second",
      isChecked : false
    }
  ]

  $scope.triggerAll = function(){
    angular.forEach($scope.objects, function(value){
      value.isChecked = !value.isChecked;
    })
  }
}

Pay attention that triggering all checkboxes is done with ngClick, not with usual onClick or jQuery handler. This allows Angular to run dirty checks and behave correctly.

like image 181
madhead - StandWithUkraine Avatar answered Sep 27 '22 23:09

madhead - StandWithUkraine