Saturday, December 29, 2012

AngularJS with input file directive

How to make +AngularJS work with input file.

Problem: input type file doesn't work with +AngularJS
<input type="file" data-ng-model="param.file" data-ng-change="change($event)"/>

This doesn't work with angular. Unfortunately. The change event is not triggered. Model is not initialized. SNAFU. But with shining +AngularJS  feature called directive it is very easy to fix it and make it work elegantly without much efforts. Let's see.
index.html

<div
   data-ng-app="angularjstutorial.blogspot.com/2012/12/angularjs-with-input-file-directive.html"
   data-ng-controller="MainController">
   <input type="file" data-file="param.file"/>
   <div>param.file: {{param.file}}</div>           
</div>

script.js

module.directive('file', function(){
    return {
        scope: {
            file: '='
        },
        link: function(scope, el, attrs){
            el.bind('change', function(event){
                var files = event.target.files;
                var file = files[0];
                scope.file = file ? file.name : undefined;
                scope.$apply();
            });
        }
    };
});

8 comments:

  1. After searching for a while i decided to use https://github.com/danialfarid/angular-file-upload
    for a simple file upload use.
    Just because it was very easy to upload the picture inside a scope function.
    a bit similar to the approach the that article.
    I would love to hear your thoughts as i'm still not sure what is the best approach.
    Thanks!

    ReplyDelete
    Replies
    1. If the library fits your needs and works then by all means use it.
      My example is very simple and covers only a way (one of millions probably) to make angular see a file change event. I didn't cover the form submission step at all, but it shouldn't very hard once you have the file in your model. I see they also provide $http.uploadFile function which is great. I'm not familiar with the implementation, but if it works fine I'd just use it and not reinvent the wheel.
      However, in some cases you may need to implement some special file processing that don't fit into a simple scenario and, hopefully, my example can be the starting point.

      Delete
  2. Good blog posts by the author
    Sanjary Academy is the best Piping Design institute in Hyderabad, Telangana. It is the best Piping design Course in India and we have offer professional Engineering Courses like Piping design Course, QA/QC Course, document controller course, Pressure Vessel Design Course, Welding Inspector Course, Quality Management Course and Safety Officer Course.
    Piping Design Course in India­

    ReplyDelete
  3. Hiiii....Thanks for sharing Great info....Nice post...Keep move on....
    Angular JS Training in Hyderabad

    ReplyDelete
  4. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    best angularjs online training
    top angularjs online training
    angularjs online training

    ReplyDelete
  5. Casinos Near Me - JTGHub
    Find all the 안동 출장안마 Casinos Near 서산 출장안마 Me in 2021 with 안양 출장안마 JTGHub's list of the 12000+ Casinos with 나주 출장샵 the closest Casinos Near Me | JTG Hub 원주 출장안마

    ReplyDelete