0

REVISION

As I have come to truly understand my question, I have modified this question in search for a better answer. Although this link was helpful for MIME types of the 'accept' attribute, my question stems deeper.

I am writing an application where I allow users to upload files, and the files must be of a specific MIME type.

The directive is used in many places, and the MIME type needs to change across these places. I need to dynamically construct the template of my directive in order to accommodate for each implementation of the directive.

Assume that I write HTML as so:

<cmc-file-upload mimeType="text/html" ng-if="reportFileUploadSettings.showFileUploader" upload-complete-callback="importReportFile(content)" ></cmc-file-upload>

I would then wish to apply that MIME type to the template of the directive:

components.directive('cmcFileUpload', function ()
{
    return {
        restrict: 'E',
        scope: {
            uploadCompleteCallback: "&",
            mimeType: "&",
        },
        replace: false,
        template: '<input type="file" id="files" name="files[]" accept="' + scope.mimeType + '" multiple /><output id="list"></output>',

Unfortunately, this code does not seem to work this way.

How can I make this work?

REFERENCE ERROR: scope is undefined...

4
  • Are you making a directive? I may want to check if extension of the file is .html or .htm. The correct validation can be done server side. Commented Jul 23, 2014 at 16:58
  • 1
    possible duplicate of File input 'accept' attribute - is it useful? Commented Jul 23, 2014 at 16:58
  • My question stems much deeper than the post that you believe I may be duplicating @dave. I have modified my question to accommodate its astronomical depth. I would hope that you take another look at my question. Commented Jul 23, 2014 at 18:01
  • I really hope someone answers me soon :(... Commented Jul 23, 2014 at 18:33

2 Answers 2

1

Change mimeType: "&", to mimeType: "@",

because you do

mimeType="text/html"

Since "text/html" is not a variable, but the raw string, you need to use @

Then, change

accept="' + scope.mimeType + '" 

to

accept="{{mimeType}}" 

because scope the variable is not defined (yet) - once Angular parses the template, it will replace mimeType with the variable.

Edit: And the final issue is that mimeType="text/html" needs to be mime-type="text/html" because in the directive when you do

mimeType: "@",

it thinks because you used camelCase it should look for mime-type

Sign up to request clarification or add additional context in comments.

14 Comments

very close, I thought that was going to be the answer for sure, but its not working. There is no filtering on the accepted MIME types. I think that Angular isn't evaluating the accept="{{mimeType}}"
Is there anyway that we can make Angular evaluate bind statements on the templateURL? I think this will work if we can
Would it make sense to attach the ng-bind atrribute to the template somehow? Sorry for my lack of understanding on this. Thanks for all the focus.
jsfiddle.net/R6VJ5 - figured it out. It was looking for the propery "mime-type" since camelCase is used in a directive to indicate dashes.
|
1

To restrict to html use this:

template: '<input type="file" accept="text/html" id="files" name="files[]" multiple /><output id="list"></output>',

a complete answer on how to use accept is here:

File input 'accept' attribute - is it useful?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.