Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS: Parent scope not updated in directive (with isolated scope) two way binding
    primarykey
    data
    text
    <p>I have the following code, which can also be fiddled on <a href="http://jsfiddle.net/garukun/u69PT/">http://jsfiddle.net/garukun/u69PT/</a>.</p> <p>View:</p> <pre><code>&lt;div data-ng-app="testApp"&gt; &lt;div data-ng-controller="testCtrl"&gt; &lt;strong&gt;{{pkey}}&lt;/strong&gt; &lt;span data-test-directive data-parent-item="pkey" data-parent-update="update(pkey)"&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>JS:</p> <pre><code>var testApp = angular.module('testApp', []); testApp.directive('testDirective', function ($timeout) { return { scope: { key: '=parentItem', parentUpdate: '&amp;' }, replace: true, template: '&lt;div&gt;&lt;p&gt;{{key}}&lt;/p&gt;' + '&lt;button data-ng-click="lock()"&gt;Lock&lt;/button&gt;' + '&lt;/div&gt;', controller: function ($scope, $element, $attrs) { $scope.lock = function () { $scope.key = 'D+' + $scope.key; console.log('DIR :', $scope.key); // Expecting $scope.$parent.pkey to have also been // updated before invoking the next line. $scope.parentUpdate(); // $timeout($scope.parentUpdate); // would work. }; } }; }); testApp.controller('testCtrl', function ($scope) { $scope.pkey = 'golden'; $scope.update = function (k) { // Expecting local variable k, or $scope.pkey to have been // updated by calls in the directive's scope. console.log('CTRL:', $scope.pkey, k); $scope.pkey = 'C+' + k; console.log('CTRL:', $scope.pkey); }; }); </code></pre> <p>Basically, I'm setting up the directive with an isolated scope, in which I'm two-way binding a property (key) from the parent scope (pkey), and also delegating a method (parentUpdate) to be called in the context of the parent scope.</p> <p>Now, during a ng-click event handler in the directive, I want to invoke the parentUpdate method and do something within. When I'm invoking that method, I'm expecting my parent scope's model to have been updated. But in reality, it is not, and this is what's puzzling me.</p> <p>It's probably because of some missing $digest cycles in the middle, since wrapping the parentUpdate call with $timeout would work as expected.</p> <p>Could someone shed some light on what's missing? Or how to properly invoked parentUpdate?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload