Angularjs

AngularJS Directive To Listen For File Input Changes

Posted by Weston Ganger on July 09, 2015

Its handy to know when a file input has been changed so you can perform some action such as create a preview image.

Here we will make a directive to handle that


myApp.directive('customOnChange', function() {
  return {
    restrict: 'A',
    link: function (scope, element, attrs) {
      var onChangeFunc = scope.$eval(attrs.customOnChange);
      element.bind('change', onChangeFunc);
    }
  };
});

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

And you can use it in your HTML like so:


<input type="file" custom-on-change="uploadFile($event)">


Related External Links:


Posted in Angular and Tagged with angularjs file-input upload directive 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts