Note that there are some explanatory texts on larger screens.

plurals
  1. POcalling method of parent controller from a directive in AngularJS
    primarykey
    data
    text
    <p>Following my previous <a href="https://stackoverflow.com/questions/15990122/passing-object-to-angularjs-directive-from-the-controller">question</a>, I'm now trying to call a method on the parent controller from my directive. I get an undefined parameter. Here's what I do:</p> <pre><code>&lt;body ng-app="myApp" ng-controller="MainCtrl"&gt; &lt;span&gt;{{mandat.rum}}&lt;/span&gt; &lt;span&gt;{{mandat.surname}}&lt;/span&gt; &lt;input type="text" ng-model="mandat.person.firstname" /&gt; &lt;my-directive mandate-person="mandat.person" updateparent="updatePerson()" &gt; &lt;/my-directive&gt; &lt;/body&gt; </code></pre> <p>And the script:</p> <pre><code>var app = angular.module('myApp', []); app.controller('MainCtrl', function ($scope) { $scope.mandat = { name: "John", surname: "Doe", person: { id: 1408, firstname: "sam" } }; $scope.updatePerson = function(person) { alert(person.firstname); $scope.mandat.person = person; } }); app.directive('myDirective', function () { return { restrict: 'E', template: "&lt;div&gt;&lt;span&gt;{{mandatePerson.id}}&lt;span&gt;&lt;input type='text' ng-model='mandatePerson.firstname' /&gt;&lt;button ng-click='updateparent({person: mandatePerson})'&gt;click&lt;/button&gt;&lt;/div&gt;", replace: true, scope: { mandatePerson: '=', updateparent: '&amp;' } } } ) </code></pre> <p>when the updatePerson method gets called, person is undefined.</p> <p>jsfiddle here : <a href="http://jsfiddle.net/graphicsxp/Z5MBf/7/" rel="noreferrer">http://jsfiddle.net/graphicsxp/Z5MBf/7/</a></p>
    singulars
    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