2

i got a code which i understand but a small area is not clear. so help me to understand please.

<div ng-controller="MainCtrl">
  <label ng-repeat="(color,enabled) in colors">
      <input type="checkbox" ng-model="colors[color]" /> {{color}} 
  </label>
  <p>colors: {{colors}}</p>

<script>
  var app = angular.module('plunker', []);

  app.controller('MainCtrl', function($scope){
      $scope.colors = {Blue: true, Orange: true};
  });
</script>

http://plnkr.co/edit/U4VD61?p=preview

see this line ng-model="colors[color]" how this line automatically set checkbox checked or unchecked state because in code we do not mention checked = true or false then how angular automatically extract true or false from model and set it in checkbox.......this is not clear. please guide me.

1

1 Answer 1

2

angular ng-model checks checkbox if ng-model value is true. You have given Blue as true. So ng-model checks the checkbox.

If you using angular ng-model it will automaticaly fills value of any form field. You don't need to put ng-checked = true or something

How to check if any Checkbox is checked in Angular

https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D

Sign up to request clarification or add additional context in comments.

3 Comments

Do you have a link that explains this ? Not that I don't believe you but I'd like to read up on that.
You could check with angularjs input checkbox page
yes i understand thanks a lot. i would mark it as answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.