AngularJS Directive To Listen For File Input Changes

Posted by Weston Ganger

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:

Article Topic:Software Development - Angular

Date:July 09, 2015

Recommended Posts