Skip to content Skip to sidebar Skip to footer

AngularJS Data Binding Checkboxes To Object If Checked

I have a JSON object which I am repeating with ng-repeat, the keys are strings, and the values are an array. I am listing each of the values as a checkbox. I would like to create a

Solution 1:

My examples have your angular logic in the recommended syntax (non-global). There were also several issues with your markup that I have corrected.

In this example, ng-model="x" is a placeholder that I don't use, but ng-model must be present or an error is thrown. I am using ng-change to handle the link between the checkboxes and $scope.outputs.

Live demo here (click).

Markup:

<div ng-app="myApp" ng-controller="myCtrl">
  <h3 >Dynamic data binding AngularJS</h3>
  <h4>Inputs</h4>
  <ul>
    <li ng-repeat="(typeKey, typeVal) in inputs">
      <span>{{typeKey}} : </span>
      <ul>
        <li ng-repeat="value in typeVal">
          <label>{{value}}
            <input
              type="checkbox"
              ng-model="x"
              ng-change="setOutput(typeKey, $index, value)"
            >
          </label>
        </li>
      </ul>
    </li>
  </ul>    

  <h4>Outputs</h4>
  <ul ng-repeat="(key,value) in inputs">
    <li>{{key}} : {{outputs[key]}}</li>
  </ul>
</div>

JavaScript:

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
  $scope.outputs = {};
  $scope.inputs = {
    'category': ['one','two','three'],
    'color':['blue','green']
  };
  $scope.setOutput = function(typeKey, $index, value) {
    $scope.outputs[typeKey] = $scope.outputs[typeKey] || [];
    $scope.outputs[typeKey][$index] = value;
  };
});

Another Solution

Live demo here (click).

First, I used ng-init to dynamically add the first-level properties from inputs to outputs. Then you just needed to set your ng-model and ng-checked properties to the correct location in outputs.

Markup:

<div ng-app="myApp" ng-controller="myCtrl">
  <h3 >Dynamic data binding AngularJS</h3>
  <h4>Inputs</h4>
  <ul>
    <li 
      ng-repeat="(typeKey, typeVal) in inputs"
      ng-init="outputs[typeKey] = outputs[typeKey] || {}">
      <span>{{typeKey}} : </span>
      <ul>
        <li ng-repeat="value in typeVal">
          <label>{{value}}
            <input
              type="checkbox"
              ng-model="outputs[typeKey][value]"
              ng-checked="outputs[typeKey][value]"
              value="outputs[typeKey][value]"
            >
          </label>
        </li>
      </ul>
    </li>
  </ul>    

  <h4>Outputs</h4>
  <ul ng-repeat="(key,value) in inputs">
    <li>{{key}} : {{outputs[key]}}</li>
  </ul>
</div>

JavaScript:

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
  $scope.outputs = {};
  $scope.inputs = {
    'category': ['one','two','three'],
    'color':['blue','green']
  };
});

Solution 2:

You need to bind to the value for the parent, as checkboxes don't work like that. Here's an example:

<h3 >Dynamic data binding in AngularJS</h3>
<div ng-app ng-controller="Controller" class="container">
    <h4>Inputs</h4>
    <ul ng-repeat="(parent, values) in inputs">
        <span>{{parent}} : </span>
        <li ng-repeat="value in values"><label>{{value}}
            <input type="checkbox" ng-model="output[parent][value]" ng+checked="output[parent][value]" value="value" >                               
            </input></label>
        </li>
    </ul>    

    <h4>Outputs</h4>
    <ul ng-repeat="(key,value) in inputs">
        <li>
            {{key}} : {{output[key]}}
        </li>
    </ul>
</div>

And in the controller create the keys beforehand

function Controller($scope) {
    $scope.output = { 'category': {}, color: {} };
    $scope.inputs = {'category': ['one','two','three'], 'color':['blue','green']};
}

jsfiddle: http://jsfiddle.net/5eeVc/


Solution 3:

Have a look at this plunk, i have handled two different scenarios. Just sharing it as a knowledge article

Plunk

      <h3>First Scenario <small>Handling JSON source </small></h3>
  <div class="container-fluid">
    <div class="row">
      <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
        <h4>Available options</h4>
        <div ng-repeat="item in itemList">
          <mark>{{item.name}}</mark>
          <input type="checkbox" ng-model="modelContainer[$index].checked" />
        </div>
      </div>
      <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
        <h4 class="text-success">Selected options</h4>
        <ul>
          <li ng-repeat="item in modelContainer | filter: {checked:true}">
            {{item.item.name}} <a href="#" class="cl" ng-click="uncheck(item.item.id)">X</a>
          </li>
        </ul>
      </div>
    </div>
  </div>
  <br>

  <p class="text-danger">itemList</p>
  <code>{{itemList}}</code>
  <br>
  <br>
  <p class="text-danger">modelContainer</p>
  <code>{{modelContainer}}</code>

  <hr>
  <h3>Second Scenario <small>Handling map Source </small></h3>
  <div class="container-fluid">
    <div class="row">
      <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
        <h4>Available options</h4>
        <div ng-repeat="item in Maplist">
          <mark>{{item}}</mark>
          <input type="checkbox" ng-model="modelContainer1[$index].checked" />
        </div>
      </div>
      <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
        <h4 class="text-success">Selected options</h4>
        <ul>
          <li ng-repeat="item in modelContainer1 | filter: {checked:true}">
            {{item.name}} <a href="#" class="cl" ng-click="uncheck1(item.id)">X</a>
          </li>`enter code here`
        </ul>
      </div>
    </div>
  </div>

Post a Comment for "AngularJS Data Binding Checkboxes To Object If Checked"