File pick with Angular JS

后端 未结 8 758
时光取名叫无心
时光取名叫无心 2020-12-08 07:50

I would like to pick up a file with AngularJS:

HTML:

8条回答
  •  借酒劲吻你
    2020-12-08 08:26

    Following is my approach with a directive.

    Directive

    angular
      .module('yourModule')
      .directive('fileChange', function() {
        return {
         restrict: 'A',
         scope: {
           handler: '&'
         },
         link: function (scope, element) {
          element.on('change', function (event) {
            scope.$apply(function(){
              scope.handler({files: event.target.files});
            });
          });
         }
        };
    });
    

    HTML

    
    

    Controller

    fileSelect = function (files) {
          var file = files[0];
          //you will get the file object here
    }
    

提交回复
热议问题