0

I'm trying to use ng-model on input element with file type. I've found this example but still dunno how to turn it to ES6 and use it (put it to ng-model or sth).

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


myApp.controller('myCtrl', function($scope) {
    $scope.uploadFile = function(){
        var filename = event.target.files[0].name;
        alert('file was selected: ' + filename);
    };
});
Przemek
  • 27
  • 3
  • 1
    What feature of ES6 are you trying to implement? The only think relevant here would maybe be a lambda function – Sterling Archer Apr 06 '16 at 14:20
  • This is a very simple approach for *reading* the file input with `ng-model`: [fiddle](https://jsfiddle.net/reLmnp82/). You should implement `ngModel.$render` to transfer the value of the model to the view (e.g. initialization), but I am not sure if this is feasible with file inputs. Also take a look at ready-made components, e.g. [ng-file-upload](https://www.npmjs.com/package/ng-file-upload). – Nikos Paraskevopoulos Apr 06 '16 at 14:27

0 Answers0