title: Angular select all checkboxes from outside ng-repeat
body: <h2>Description</h2>
  
  <p>I have a small product order system, where a user can add order lines, and on each order line add one or more products. (I realise it's quite unusual for more than one product to be on the same order line, but that's another issue).</p>
  
  <p>The products that can be selected on each line is based on a hierarchy of products. For example:</p>
  
  <h2>Example product display</h2>
  
  <pre><code>T-Shirts
     V-neck
     Round-neck
     String vest
  </code></pre>
  
  <h2>JSON data</h2>
  
  <pre><code>$scope.products = [
  { 
    id: 1, 
    name: 'T Shirts', 
    children: [
      { id: 4, name: 'Round-neck', children: [] },
      { id: 5, name: 'V-neck', children: [] },
      { id: 6, name: 'String vest (exclude)', children: [] }
    ] 
  },
  { 
    id: 2, 
    name: 'Jackets', 
    children: [
      { id: 7, name: 'Denim jacket', children: [] },
      { id: 8, name: 'Glitter jacket', children: [] }
    ] 
  },
  { 
    id: 3, 
    name: 'Shoes', 
    children: [
      { id: 9, name: 'Oxfords', children: [] },
      { id: 10, name: 'Brogues', children: [] },
      { id: 11, name: 'Trainers (exclude)', children: []}
    ] 
  }
  </code></pre>
  
  <p>];</p>
  
  <p>T-Shirts isn't selectable, but the 3 child products are.</p>
  
  <h2>What I'm trying to achieve</h2>
  
  <p>What I'd like to be able to do, is have a 'select all' button which automatically adds the three products to the order line.</p>
  
  <p>A secondary requirement, is that when the 'select all' button is pressed, it excludes certain products based on the ID of the product. I've created an 'exclusion' array for this.</p>
  
  <p>I've set up a <a href="http://plnkr.co/edit/XsbyM0tye4vYGFhb8ygG?p=preview" rel="nofollow">Plunker</a> to illustrate the shopping cart, and what I'm trying to do. </p>
  
  <p>So far it can:</p>
  
  <ul>
  <li>Add / remove order lines</li>
  <li>Add / remove products</li>
  <li>Add a 'check' for all products in a section, excluding any that are in the 'exclusions' array</li>
  </ul>
  
  <h2>The problem</h2>
  
  <p>However, although it adds the check in the input, it doesn't trigger the ng-change on the input:</p>
  
  <pre><code>&lt;table class="striped table"&gt;
      &lt;thead&gt;
        &lt;tr&gt;
          &lt;td class="col-md-3"&gt;&lt;/td&gt;
          &lt;td class="col-md-6"&gt;&lt;/td&gt;
          &lt;td class="col-md-3"&gt;&lt;a ng-click="addLine()" class="btn btn-success"&gt;+ Add order line&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr ng-repeat="line in orderHeader.lines"&gt;
          &lt;td class="col-md-3"&gt;
  
            &lt;ul&gt;
              &lt;li ng-repeat="product in products" id="line_{{ line.no }}_product_{{ product.id }}"&gt;
  
                {{ product.name }} &lt;a ng-click="selectAll(product.id, line.no)" class="btn btn-primary"&gt;Select all&lt;/a&gt;
  
                &lt;ul&gt;
                 &lt;li ng-repeat="child in product.children"&gt;
                   &lt;input type="checkbox" 
                        ng-change="sync(bool, child, line)" 
                        ng-model="bool" 
                        data-category="{{child.id}}" 
                        id="check_{{ line.no }}_product_{{ child.id }}"
                        ng-checked="isChecked(child.id, line)"&gt;
                {{ child.name }}
                 &lt;/li&gt; 
                &lt;/ul&gt;
  
              &lt;/li&gt;
            &lt;/ul&gt;
          &lt;/td&gt;
          &lt;td class="col-md-6"&gt;
            &lt;pre style="max-width: 400px"&gt;{{ line }}&lt;/pre&gt;
          &lt;/td&gt;
          &lt;td class="col-md-3"&gt;
            &lt;a ng-click="removeLine(line)" class="btn btn-warning"&gt;Remove line&lt;/a&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  </code></pre>
  
  <h2>Javascript</h2>
  
  <pre><code>$scope.selectAll = function(product_id, line){
  
    target = document.getElementById('line_'+line+'_product_'+product_id);
  
    checkboxes = target.getElementsByTagName('input');
  
    for (var i = 0; i &lt; checkboxes.length; i++) {
      if (checkboxes[i].type == 'checkbox') {
  
        category = checkboxes[i].dataset.category;
  
        if($scope.excluded.indexOf(parseInt(category)) == -1)
        {
          checkboxes[i].checked = true;
          // TODO: Check the checkbox, and set its bool parameter to TRUE     
        }
      }
    }
  }
  </code></pre>
  
  <h1>Update with full solution</h1>
  
  <p>There were a couple of issues with the above code. Firstly, I was trying to solve the problem by manipulating the DOM which is very much against what Angular tries to achieve.</p>
  
  <p>So the solution was to add a 'checked' property on the products so that I can track if they are contained on the order line, and then the view is updated automatically.</p>
  
  <p>One drawback of this method is that the payload would be significantly larger (unless it is filtered before being sent to the back-end API) as each order line now has data for ALL products, even if they aren't selected.</p>
  
  <p>Also, one point that tripped me up was forgetting that Javascript passes <code>references</code> of objects / arrays, not a new copy.</p>
  
  <h2>The solution</h2>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code> var myApp = angular.module('myApp', []);
  
  myApp.controller('CartForm', ['$scope', function($scope) {
  
    var inventory = [
  { 
    id: 1, 
    name: 'T Shirts',
    checked: false, 
    children: [
      { id: 4, name: 'Round-neck', checked: false, children: [] },
      { id: 5, name: 'V-neck', checked: false, children: [] },
      { id: 6, name: 'String vest (exclude)', checked: false, children: [] }
    ] 
  },
  { 
    id: 2, 
    name: 'Jackets',
    checked: false, 
    children: [
      { id: 7, name: 'Denim jacket', checked: false, children: [] },
      { id: 8, name: 'Glitter jacket', checked: false, children: [] }
    ] 
  },
  { 
    id: 3, 
    name: 'Shoes', 
    checked: false, 
    children: [
      { id: 9, name: 'Oxfords', checked: false, children: [] },
      { id: 10, name: 'Brogues', checked: false, children: [] },
      { id: 11, name: 'Trainers (exclude)', checked: false, children: []}
    ] 
  }
     ];
  
    $scope.debug_mode = false;
  
  
    var products = angular.copy(inventory);
  
    $scope.orderHeader = {
  order_no: 1,
  total: 0,
  lines: [
    {
      no: 1,
      products: products,
      total: 0,
      quantity: 0
    }
  ]
    };
  
  
    $scope.excluded = [6, 11];
  
     $scope.addLine = function() {
  
   var products = angular.copy(inventory);
  
    $scope.orderHeader.lines.push({
        no: $scope.orderHeader.lines.length + 1,
        products: products,
        quantity: 1,
        total: 0
    });
  
    $scope.loading = false;
  
  }
  
  
      $scope.removeLine = function(index) {
    $scope.orderHeader.lines.splice(index, 1);
  }  
  
  
  $scope.selectAll = function(product){
  
    angular.forEach(product.children, function(item){
      if($scope.excluded.indexOf(parseInt(item.id)) == -1) {
          item.checked=true;
      }
    });
  
  }
  
  $scope.removeAll = function(product){
  
    angular.forEach(product.children, function(item){
      item.checked=false;
    });
  
  }
  
  $scope.toggleDebugMode = function(){
    $scope.debug_mode = ($scope.debug_mode ? false : true);
  }
  
  
  }]);
  </code></pre>
  
  <p><strong><a href="http://plnkr.co/edit/B66d6phgTIIY4hMabWMD?p=preview" rel="nofollow">Click here to see the Plunker</a></strong></p>