title: ng-repeat within directive will not update when adding or modifying objects to the existing collection
body: <p>I've got a treeview which contains objects with nested arrays that I am trying to populate. </p>
  
  <p><a href="http://plnkr.co/edit/drC46ZOqWviM0PVVVzy4?p=preview" rel="nofollow">A working plunker may be found here.</a></p>
  
  <p>Here is a breakdown of how the code is structured and what I have tried thus far.</p>
  
  <p>Within my treeview.html view I am loading the treeview directive I have created:
  I currently have two just to test the bindings. The second one will not populate until the incident checkbox has been checked.</p>
  
  <pre><code>&lt;H3&gt;Tree View Sameple&lt;/H3&gt;
  
  &lt;div&gt;
    &lt;h3&gt;Treeview 1&lt;/h3&gt;
      &lt;tree src="tree.treeData" iobj="object" filter="tree.getAggregateInfo(object, isSelected)"&gt;&lt;/tree&gt;
      &lt;h3&gt;Treeview 2 once object has been updated&lt;/h3&gt;
      &lt;tree src="tree.treeData2" iobj="object" filter="tree.getAggregateInfo(object, isSelected)"&gt;&lt;/tree&gt;
  &lt;/div&gt;
  &lt;hr/&gt;
  &lt;p&gt;javascript object data&lt;/p&gt;
  {{tree.treeData}}
  </code></pre>
  
  <p>The bottom binding is just showing the output of the js file. Wihtin the treeview when you click on the incident checkbox I am simulating a new nested collection being retrieved that should be inserted as a child element under the incident node of the tree.</p>
  
  <p>Here is the code used in the treeviewController.js</p>
  
  <pre><code>(function () {
  
      angular.module('app')
             .controller('treeviewController', ['$log', '$scope', '$timeout', treeviewController]);
      function treeviewController($log, $scope, $timeout) {
  
          var vm = this;
         //storage container for last node selected in tree
          vm.lastNodeSelected = '';
  
          vm.treeData2 = {};//test container to reflect object once updated
          //original data not an arry but an object holding an array
          vm.treeData = 
              {
                  "children": [
                    {
                        "name": "Document Type",
                        "children": [
                          {
                              "name": "Incident",
                              "documentType": "Document Type",
                              "aggregateUponField": "_ocommon.documenttype",
                              "parent": "",
                              "count": 2950,
                              "children": null
                          },
                          {
                              "name": "Some Event",
                              "documentType": "Document Type",
                              "aggregateUponField": "_ocommon.documenttype",
                              "parent": "",
                              "count": 2736,
                              "children": [{
                                          "name": "Some Event Date",
                                          "children": [
                                                      {
                                                      "name": "2008",
                                                      "documentType": "Incident Date",
                                                      "aggregateUponField": "dateincidentstart",
                                                      "parent": "",
                                                      "count": 451,
                                                      "children": null
                                                      },
                                                      {
                                                      "name": "2009",
                                                      "documentType": "Incident Date",
                                                      "aggregateUponField": "dateincidentstart",
                                                      "parent": "",
                                                      "count": 407,
                                                     "children": null
                                                     },
                                                     {
                                                     "name": "2010",
                                                     "documentType": "Incident Date",
                                                     "aggregateUponField": "dateincidentstart",
                                                     "parent": "",
                                                     "count": 426,
                                                     "children": null
                                                     }
                                                    ]
                                         }]
                          }
                        ]
                    }
                  ]
              };
  
          //aggregate data to be nested under incident
          vm.newChildData = [
                {
                    "name": "Incident Date",
                    "children": [
                      {
                          "name": "2008",
                          "documentType": "Incident Date",
                          "aggregateUponField": "dateincidentstart",
                          "parent": "",
                          "count": 451,
                          "children": null
                      },
                      {
                          "name": "2009",
                          "documentType": "Incident Date",
                          "aggregateUponField": "dateincidentstart",
                          "parent": "",
                          "count": 407,
                          "children": null
                      },
                      {
                          "name": "2010",
                          "documentType": "Incident Date",
                          "aggregateUponField": "dateincidentstart",
                          "parent": "",
                          "count": 426,
                          "children": null
                      }
                    ]
                } 
  
          ];
  
  
          var loadData = function () {
              //stip out array
              var children = vm.treeData.children;
              //search array for matching parent
              $log.info(vm.lastNodeSelected);
              var cnt = children.length;
              for (var i = 0; i &lt; cnt; i++) {
                  if (children[i].children) {
                      var innerCnt = children[i].children.length;
                      for (var c = 0; c &lt; innerCnt; c++) {
                          if (children[i].children[c].name === vm.lastNodeSelected) {
                              children[i].children[c].children = vm.newChildData;
  
                          }
                      }
                  }
              }
  
              //wrap back in object and assign to ngModel
              vm.updatedList = { children };
              // $log.info(updatedList);      
  
              vm.treeData2 = vm.updatedList;
              $timeout(function() {
                  $scope.$apply(function() {
                      vm.treeData = vm.updatedList;
                  })} , 0);
          }
  
          vm.getAggregateInfo = function (node) {
  
              vm.lastNodeSelected = node.name;
              loadData();
             // $log.info(vm.lastNodeSelected);
          };
  
  
      }
  
  })();
  </code></pre>
  
  <p>Following is the treeview directive.</p>
  
  <pre><code>angular.module('app')
      .directive('tree', function () {
          return {
              restrict: 'E',
              replace: true,
              scope: {
                  t: '=src',
                  filter: '&amp;'
              },
              template: '&lt;ul&gt;&lt;branch ng-repeat="c in t.children track by $index"  src="c" filter="filter({ object: object, isSelected: isSelected })"&gt;&lt;/branch&gt;&lt;/ul&gt;'
  
          };
      });
  
  angular.module('app')
  .directive('branch', function ($compile) {
  
      return {
          restrict: 'E',
          replace: true,
          scope: {
              b: '=src',
              filter: '&amp;',
              checked: '=ngModel'
          },
  
          template: '&lt;li class="dir-tree"&gt;&lt;input type="checkbox" ng-click="innerCall()" ng-model="b.$$hashKey" ng-change="stateChanged(b.$$hashKey)"  /&gt;{{ b.name }}  &lt;span ng-hide="visible"&gt; ({{ b.count }})&lt;/span&gt;&lt;/li&gt;',
          link: function (scope, element, attrs) {
  
              var clicked = '';
              var hasChildren = angular.isArray(scope.b.children);
              scope.visible = hasChildren;
              if (hasChildren) {
                  element.append('&lt;tree src="b" filter="filter({ object: object, isSelected: isSelected })"&gt;&lt;/tree&gt;');
                  $compile(element.contents())(scope);
              }
              element.on('click', function (event) {
                  event.stopPropagation();
                  if (hasChildren) {
                      element.toggleClass('collapsed');
                  }
              });
              scope.stateChanged = function (b) {
                  clicked = b;
              };
  
              scope.innerCall = function () {
                  scope.filter({ object: scope.b, isSelected: clicked });
              };
          }
      };
  });
  </code></pre>
  
  <p>What I am seeing is when I click on the incident check box the javascript object does update. You can see this in the output as well as the second treeview that gets bound once the check box is selected.</p>
  
  <p>I suspect the issue is related to the $digest cycle of the event and the tree is not being notified to actually update. I've tried as you can see in the controller using a <code>$scope.$apply()</code> wrapped in a timeout but this has not worked to update the original tree. </p>
  
  <p>Doing additive actions like a <code>.push()</code> to the array updates however trying to insert a new object into an existing array does not seem to work.</p>
  
  <p>Any suggestions on how to get the tree to update and reflect the child objects?</p>