AngularJs: How to check for changes in file input fields?

前端 未结 15 1381
傲寒
傲寒 2020-11-22 09:06

I am new to angular. I am trying to read the uploaded file path from HTML \'file\' field whenever a \'change\' happens on this field. If i use \'onChange\' it works but when

15条回答
  •  自闭症患者
    2020-11-22 09:47

    Working Demo of "files-input" Directive that Works with ng-change1

    To make an element work the ng-change directive, it needs a custom directive that works with the ng-model directive.

    
    

    The DEMO

    angular.module("app",[])
    .directive("filesInput", function() {
      return {
        require: "ngModel",
        link: function postLink(scope,elem,attrs,ngModel) {
          elem.on("change", function(e) {
            var files = elem[0].files;
            ngModel.$setViewValue(files);
          })
        }
      }
    })
    
    .controller("ctrl", function($scope) {
         $scope.onInputChange = function() {
             console.log("input change");
         };
    })
    
      
        

    AngularJS Input `type=file` Demo

    Files

    {{file.name}}

提交回复
热议问题