Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a <a href="http://jsfiddle.net/mrajcok/tsSJA/" rel="nofollow">fiddle</a> with two versions of the directive. One does not create a new scope. The other creates an isolate scope. What I found out is that ng-click can't contain {{}}s, so we need to use a compile function to get the URL string into the template, via the attributes argument (tAttrs).</p> <p><strong>No new scope</strong>. HTML:</p> <pre><code>&lt;uploader url="http://someplace.com/pic.jpg" upload-fn="upload"&gt;&lt;/uploader&gt; </code></pre> <p>Directive:</p> <pre><code>myApp.directive('uploader', function () { return { restrict: 'E', templateUrl: "/assets/uploader.html", compile: function (tElement, tAttrs) { var buttonElement = tElement.find('button') buttonElement.attr('ng-click', tAttrs.uploadFn + "('" + tAttrs.url + "')") console.log(tElement.html()) } } }); </code></pre> <p>Above, the directive uses the same scope as the controller, so the ng-click attribute's value is set to call the function defined by the upload-fn attribute in the HTML. </p> <p>The upload-fn attribute is not necessary, but it does make the directive more reusable, since the directive is told what scope method to call -- "upload" -- and hence can be easily altered in the HTML. If reusablility is not a concern, here's the simpler alternative:</p> <p>HTML:</p> <pre><code>&lt;uploader url="http://someplace.com/pic.jpg"&gt;&lt;/uploader&gt; </code></pre> <p>Directive change:</p> <pre><code>buttonElement.attr('ng-click', "upload('" + tAttrs.url + "')") </code></pre> <p><strong>Isolate scope</strong>. HTML:</p> <pre><code>&lt;uploader-isolate url="http://someplace.com/pic.jpg" upload-fn="upload(url)"&gt; &lt;/uploader-isolate&gt; </code></pre> <p>Directive:</p> <pre><code>myApp.directive('uploaderIsolate', function () { return { restrict: 'E', scope: { uploadFn: '&amp;' }, templateUrl: "/assets/uploader.html", compile: function (tElement, tAttrs) { var buttonElement = tElement.find('button') buttonElement.attr('ng-click', "uploadFn({url:'" + tAttrs.url + "'})") console.log(tElement.html()) } } }); </code></pre> <p>Above, the directive creates a new scope -- an <em>isolate</em> scope. This scope does not prototypically inherit from the parent/controller scope, so it (and the directive's template) do not have access to the upload() function defined on the controller's $scope. The '&amp;' notation is used to create a "delegate" uploadFn function. What that means is that when "uploadFn" is used in the isolate scope (i.e., in the directive's template), it will actually call the "upload" function on the parent scope. </p> <p>In addition, we specify that the upload function takes one argument, <em>url</em>. When the delegate function is used in the directive's template, we can't just pass an argument. I.e., this won't work: uploadFn('http://...'). Instead, we need to use a map/object to specify any argument values. E.g., uploadFn({url: 'http://...'}).</p> <p>The isolate scope object hash does not contain <code>url: '@'</code> because I don't think attribute ng-click's value can contain <code>{{}}</code>s. This is why we use a compile function and obtain the url attribute's value via the compile function's attribute argument. If the url value was only used elsewhere in the template, such as <code>&lt;span&gt;url = {{url}}&lt;/span&gt;</code>, then the '@' notation would be used.</p> <p>The template was altered to remove the value for the ng-click attribute. The value is assigned to the attribute in the compile function.</p> <pre><code>&lt;button class="button small" ng-click&gt;Upload File&lt;/button&gt; </code></pre>
    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.
    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