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

前端 未结 15 1529
傲寒
傲寒 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:38

    I made a small directive to listen for file input changes.

    View JSFiddle

    view.html:

    
    

    controller.js:

    app.controller('myCtrl', function($scope){
        $scope.uploadFile = function(event){
            var files = event.target.files;
        };
    });     
    

    directive.js:

    app.directive('customOnChange', function() {
      return {
        restrict: 'A',
        link: function (scope, element, attrs) {
          var onChangeHandler = scope.$eval(attrs.customOnChange);
          element.on('change', onChangeHandler);
          element.on('$destroy', function() {
            element.off();
          });
    
        }
      };
    });
    

提交回复
热议问题