0

I have a file <input /> and a <button>, with a click handler assigned to the button.

What I would like to do is to execute the click handler on the submit button when the selected file changes on the file input.

My code currently looks like this:

angular.module('myapp', [])
  .controller('MyController', function($scope) {
    $scope.clickMe= function(){
         alert("File Submitted!");  
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myapp">
<div ng-controller="MyController">
    <input type = "file">
    <div><button ng-click="clickMe()">Submit</button></div>
</div>
</body>
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
jade
  • 37
  • 5

1 Answers1

1

If I understand your question correctly, then you should find that the logic fired when the submit button is clicked, can instead be automatically invoked when a file is picked on your <input type="file" /> element by updating your template as follows:

<input type="file" onchange="angular.element(this).scope().clickMe(this)"> 

This will cause the clickMe() function on the $scope object of the enclosing controller MyController, to be called. Here's a complete example (with submit button removed seeing it's redundant):

angular.module('myapp', [])
  .controller('MyController', function($scope) {
    $scope.clickMe = function() {
      alert("File Submitted!");
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.7/angular.min.js"></script>

<body ng-app="myapp">
  <div ng-controller="MyController">
    <input type="file" onchange="angular.element(this).scope().clickMe(this)"> 
  </div>
</body>
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65