title: angular directive (2-way-data-binding) - parent is not updated via ng-click
body: <p>I have a nested directive with an isolated scope. An Array of objects is bound to it via 2 way data binding.</p>
  
  <pre><code>.directive('mapMarkerInput',['mapmarkerService', '$filter', '$timeout',function(mapMarkerService, $filter, $timeout) {
      return {
          restrict: 'EA',
          templateUrl:'templates/mapmarkerInputView.html',
          replace: true,
          scope: {
              mapmarkers: '='
          },
          link: function($scope, element, attrs) {
  
              //some other code
  
              $scope.addMapmarker = function($event) {
                  var mapmarker = {};
                  var offsetLeft = $($event.currentTarget).offset().left,
                      offsetTop = $($event.currentTarget).offset().top;
  
                  mapmarker.y_coord = $event.pageY - offsetTop;
                  mapmarker.x_coord = $event.pageX - offsetLeft;
                  mapmarker.map = $scope.currentMap;
                  $scope.mapmarkers = $scope.mapmarkers.concat(mapmarker);
              };
  
              $scope.deleteMapmarker = function(mapmarker) {
                  var index = $scope.mapmarkers.indexOf(mapmarker);
                  if(index !== -1) {
                      $scope.mapmarkers.splice(index,1);
                  }
              };
  
              //some other code
         )
  }]);
  </code></pre>
  
  <p>These 2 functions are triggered via ng-click:</p>
  
  <pre><code>&lt;img ng-if="currentMap" ng-click="addMapmarker($event)" ng-src="/xenobladex/attachment/{{currentMap.attachment.id}}" /&gt;
  &lt;div class="mapmarker-wrapper" ng-repeat="mapmarker in shownMapmarkers" ng-click="setZIndex($event)" style="position: absolute; top: {{mapmarker.y_coord}}px; left: {{mapmarker.x_coord}}px;"&gt;
       &lt;!-- some other code --&gt;
       &lt;div class="form-group"&gt;
            &lt;label&gt;Name:&lt;/label&gt;
            &lt;input ng-model="mapmarker.name" value="mapmarker.name" class="form-control" type="text"&gt;
       &lt;/div&gt;
       &lt;div class="form-group"&gt;
            &lt;label&gt;Description:&lt;/label&gt;
            &lt;input ng-model="mapmarker.description" value="mapmarker.description" class="form-control" type="text"&gt;
       &lt;/div&gt;
       &lt;button class="btn btn-danger" ng-click="deleteMapmarker(mapmarker)"&gt;Delete&lt;/button&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>As you can see I am binding the name and description directly via ng-model and that works just fine. The properties are also available in the parent scope, but neither the delete nor the add works (its changed within the directives scope, but not the parent scope).</p>
  
  <p>As far as I understand these changes should be applied, because I'm calling these functions via ng-click and I have other examples where this works. The only difference is, that I am binding to an array of objects and not a single object / property.</p>
  
  <p>I tried using <code>$timer</code> and <code>updateParent()</code> (<code>$scope.$apply()</code> does not work -> throws an exception that the function is already within the digest cycle) but with no success, so it looks like these changes are not watched at all.</p>
  
  <p>The directive code looks like this:</p>
  
  <pre><code>&lt;map-marker-input ng-if="$parent.formFieldBind" mapmarkers="$parent.formFieldBind"&gt;&lt;/map-marker-input&gt;
  </code></pre>
  
  <p>It is nested within a custom form field directive which gets the correct form field template dynamically and has therefore <code>template: '&lt;div ng-include="getTemplate()"&gt;&lt;/div&gt;'</code> as template, which creates a new child scope - that's why the <code>$parent</code> is needed here.</p>
  
  <p>The binding definitely works in one way, the expected data is available within the directive and if I'm logging the data after changing it via delete or add, it's also correct, but only from the inside of the directive.</p>
  
  <p>Because <code>ng-model</code> works I guess there might be a simple solution to the problem.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I created a plunkr with a simplified version:
  <a href="http://plnkr.co/85oNM3ECFgCzyrSPahIr" rel="nofollow">http://plnkr.co/85oNM3ECFgCzyrSPahIr</a></p>
  
  <p>Just click anywhere inside the blue area and new points are added from within the mapmarker directive. Right now I dont really prevent adding points if you delete or edit these - so you'll end up with a lot of points fast ;-)</p>
  
  <p>There is a button to show the data from the parent scope and from the child scope. </p>
  
  <p>If you edit the name or description of the <strong>one existing point</strong> that will also be changed in the parent scope (bound via <code>ng-model</code>). But all new points or deletions are ignored (bound within the functions called via <code>ng-click</code>).</p>