Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just wanted to update this with a solution: Here's the Plunk - <a href="http://plnkr.co/edit/m2YKLGGLv88jNkkgAYPp?p=preview" rel="nofollow">http://plnkr.co/edit/m2YKLGGLv88jNkkgAYPp?p=preview</a></p> <p>Basically I extended the solution from CodeZilla above using a scope variable in an Array. </p> <pre><code>$scope.func = []; </code></pre> <p>The name of the function to be called is then passed in the directive attribute functionname.</p> <pre><code>&lt;div mydir name="dir1" functionname="sayhello1"&gt;Directive dir1&lt;/div&gt; </code></pre> <p>The functions to be called from the directive are then added to the controller:</p> <pre><code>$scope.callDir1 = function (){ $scope.func['sayhello1'](); } $scope.callDir2 = function (){ $scope.func['sayhello2'](); } </code></pre> <p>In the directive itself, we use compile to create the function within the directive that will be called from the controller.</p> <pre><code>app.directive('mydir', function () { return { scope: true, compile: function (tElement, tAttrs, transclude) { return function postLink(scope, iElement, iAttrs, controller) { scope.$parent.func[iAttrs.functionname] = function (){ alert("update something in " + iAttrs.name); } } } } }); </code></pre> <p>Alternatively, you could use the link function - it works the same:</p> <pre><code>app.directive('mydir', function () { return { scope: true, link: function (scope, element, attrs) { scope.$parent.func[attrs.functionname] = function () { console.debug("update something in " + attrs.name); } } } }); </code></pre> <p>That's it. </p>
 

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