1

I am creating a file upload directive to use in forms.

angular.module("app")
    .directive("myFileUpload", [function () {
        return {
            restrict: "E",
            template: '<input type="file" multiple class="form-control" placeholder="Select file">',
            scope: {
                files: "="
            },
            link: function (scope, element) {

                element.change(function (event) {
                    scope.files = event.target.files;
                    event.preventDefault();
                });

            }
        }
    }]);

I can use this directive in somewhere of my application.

<div class="col-sm-9">
    <input ng-model="myform.title">
    <my-file-upload files="myform.files"></my-file-upload>
</div>

And I want to list that selected files like this:

<div class="col-sm-9">
    <input ng-model="myform.title">
    <my-file-upload files="myform.files"></my-file-upload>
    <p ng-repeat="item in myform.files">{{item.filename}}</p>
</div>

If I do not enter text in input myform.title, the files does not list.

1
  • Try calling scope.$apply() after assigning scope.files Commented Jan 11, 2016 at 22:06

1 Answer 1

1

the callback of

element.change

happens outside of the "angular-world". you will need to let angular know that "something happened" try this instead

element.change(function (event) {
  scope.files = event.target.files;
  event.preventDefault();
  scope.$apply();
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.