Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:58:21+00:00 2026-06-17T16:58:21+00:00

I am trying to create a directive for plupload. To take things one step

  • 0

I am trying to create a directive for plupload. To take things one step at a time I first got the file upload working without the use of the directive (controller and std html). I then added the directive and successfully got the html to render out properly. Unfortunately, the plupload binding then broke.

At first glance it seems that the directive is not rendering before the plupload does its binding, however if I add log statements within the code, the directive does return the hash and all html elements do seem to exist within the DOM.

Any ideas on how to make this work?

html

<div ng-controller="ProfilePhotoUploader">
    This does not work
    <uploader url="<%= profile_image_uploader_path %>"></uploader>

    This works
    <div id="profile-image-container">
        <div id="select-file">Select File</div>
        <div id="drop-area">Drop file here</div>
        <button class="button small" ng-click="upload('<%= profile_image_uploader_path %>')">Upload File</button>
    </div>
  </div>

uploader.js

var uploader = angular.module('uploader', []);

uploader.directive('uploader', function () {
alert("in directive");
return  {
    restrict: 'E',
    scope: {
        url:'@'
    },
    templateUrl: "/assets/uploader.html"
}
});

assets/uploader.html

<div id="profile-image-container">
  <div id="select-file">Select File</div>
  <div id="drop-area">Drop file here</div>
  <button class="button small" ng-click="upload('{{url}}')">Upload File</button>
</div>

profile_photo_uploader.js

var ProfilePhotoUploader = function ($scope) {

console.log($("uploader"));
$scope.uploader = new plupload.Uploader({
    runtimes: "html5",
    browse_button: 'select-file',
    max_file_size: '10mb',
    container: "profile-image-container",
    drop_element: "drop-area",
    multipart: true,
    multipart_params : {
        authenticity_token: $('meta[name="csrf-token"]').attr('content'),
        _method: 'PUT'
    },
    filters: [
        {title: "Image Files", extensions: "jpg,jpeg,png"}
    ]
});

$scope.upload = function(url) {
    $scope.uploader.settings.url = url;
    $scope.uploader.start();
};

$scope.uploader.init();
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:58:22+00:00Added an answer on June 17, 2026 at 4:58 pm

    Here is a fiddle 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).

    No new scope. HTML:

    <uploader url="http://someplace.com/pic.jpg" upload-fn="upload"></uploader>
    

    Directive:

    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())
            }
        }
    });
    

    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.

    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:

    HTML:

    <uploader url="http://someplace.com/pic.jpg"></uploader>
    

    Directive change:

    buttonElement.attr('ng-click', "upload('" + tAttrs.url + "')")
    

    Isolate scope. HTML:

    <uploader-isolate url="http://someplace.com/pic.jpg" upload-fn="upload(url)">
    </uploader-isolate>
    

    Directive:

    myApp.directive('uploaderIsolate', function () {
        return {
            restrict: 'E',
            scope: {
                uploadFn: '&'
            },
            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())
            }
        }
    });
    

    Above, the directive creates a new scope — an isolate 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 ‘&’ 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.

    In addition, we specify that the upload function takes one argument, url. 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://…’}).

    The isolate scope object hash does not contain url: '@' because I don’t think attribute ng-click’s value can contain {{}}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 <span>url = {{url}}</span>, then the ‘@’ notation would be used.

    The template was altered to remove the value for the ng-click attribute. The value is assigned to the attribute in the compile function.

    <button class="button small" ng-click>Upload File</button>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create a file upload directive using jQuery File Upload plugin and this
I am trying to create a directive that would load a template. The template
I'm trying to create a dynamic menu bar using struts2 url tag. I got
Im trying to create my first WCF restful service. In VS2010 I open weddingservice.svc
My Google-fu is failing me on this one... I'm trying to create an Apache
I'm trying to set up WAMP server. I've got Apache working correctly, and I've
I'm trying to create a T4 template which inherits from another one that is
I am trying to create a .spec file, and I put ChangeLog, README, INSTALL,
I am trying to create a upload component but my view isn't updating when
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.