ng-model for `<input type=“file”/>` (with directive DEMO)

后端 未结 12 2264
北恋
北恋 2020-11-21 06:40

I tried to use ng-model on input tag with type file:


But after selecting a file, in

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 07:03

    For multiple files input using lodash or underscore:

    .directive("fileread", [function () {
        return {
            scope: {
                fileread: "="
            },
            link: function (scope, element, attributes) {
                element.bind("change", function (changeEvent) {
                    return _.map(changeEvent.target.files, function(file){
                      scope.fileread = [];
                      var reader = new FileReader();
                      reader.onload = function (loadEvent) {
                          scope.$apply(function () {
                              scope.fileread.push(loadEvent.target.result);
                          });
                      }
                      reader.readAsDataURL(file);
                    });
                });
            }
        }
    }]);
    

提交回复
热议问题