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


angular.module('app', [])
  .controller('MainCtrl', MainCtrl)
  .directive('fileChange', fileChange);

  function MainCtrl($scope) {
    $scope.upload = function () {
      // do something with the file
      alert($scope.file.name);
    };
  }

  function fileChange() {
    return {
      restrict: 'A',
      require: 'ngModel',
      scope: {
        fileChange: '&'
      },
      link: function link(scope, element, attrs, ctrl) {
        element.on('change', onChange);

        scope.$on('destroy', function () {
          element.off('change', onChange);
        });

        function onChange() {
          ctrl.$setViewValue(element[0].files[0]);
          scope.fileChange();
        }
      }
    };
  }
<div ng-app="app" ng-controller="MainCtrl">
  <input type="file" file-change="upload()" ng-model="file">
</div>
Url: https://github.com/angular/angular.js/issues/1375

Comments

Popular posts from this blog

Loading App Configuration from API Before Application Start in Flutter Using Provider, Similar to Angular's APP_INITIALIZER

When to Use Flutter's FutureBuilder and Consumer for Data Retrieval from Provider?