Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Youre pretty close..</p> <pre><code>app.directive('mytag',function() { return { restrict: 'E', template: '&lt;div&gt;' + '&lt;input ng-model="controltype"/&gt;' + '&lt;button ng-click="controlfunc()"&gt;Parent Func&lt;/button&gt;' + '&lt;p&gt;{{controlval}}&lt;/p&gt;' + '&lt;/div&gt;', scope: { /* make typeattribute="whatever" bind two-ways (=) $scope.whatever from the parent to $scope.controltype on this directive's scope */ controltype: '=typeattribute', /* reference a function from the parent through funcattribute="somefunc()" and stick it our directive's scope in $scope.controlfunc */ controlfunc: '&amp;funcattribute', /* pass a string value into the directive */ controlval: '@valattribute' }, controller: function($scope) { } }; }); &lt;div ng-controller="ParentCtrl"&gt; &lt;!-- your directive --&gt; &lt;mytag typeattribute="parenttype" funcattribute="parentFn()" valattribute="Wee, I'm a value"&gt;&lt;/mytag&gt; &lt;!-- write out your scope value --&gt; {{parenttype}} &lt;/div&gt; app.controller('ParentCtrl', function($scope){ $scope.parenttype = 'FOO'; $scope.parentFn = function() { $scope.parenttype += '!!!!'; } }); </code></pre> <p>The magic is mostly in the <code>scope:</code> declaration in your directive definition. having any <code>scope: {}</code> in there will "isolate" the scope from the parent, meaning it gets it's own scope... without that, it would use the parent's scope. The rest of the magic is in the scope's properties: <code>scope: { 'internalScopeProperty' : '=externalAttributeName' }</code>... where the <code>=</code> represents a two way binding scenario. If you change that <code>=</code> to a <code>@</code> you'll see it just allows you to pass a string as an attribute to the directive. The <code>&amp;</code> is for executing functions from the parent scope's context.</p> <p>I hope that helps.</p> <hr> <p><strong>EDIT: <a href="http://plnkr.co/edit/SJCVe6">Here is a working PLNKR</a></strong></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