1

I am new to angular js and I want to disable upload button until the file is selected. According to the below code, the button is disabled but it is not getting enabled after the file is selected.

Please help me.

This is my html code:

<input type="file" id="upload" name="file" class="uploadFile" required="required" ng-model="fileToUpload">

<input type="submit" id ="submitFile" value="PostUsingAjax" onClick="uploadFile()"
                        ng-disabled = "form.$invalid || disabled" ng-click ="disableButton()">
user123
  • 259
  • 2
  • 6
  • 23
  • Possible duplicate of [Disable upload button when no file selected](https://stackoverflow.com/questions/37845503/disable-upload-button-when-no-file-selected) – vrintle Jun 03 '19 at 17:56

1 Answers1

0

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app>
  <div>
    <form name="myForm">
      <input type="file" id="upload" name="file" class="uploadFile" required ng-model="fileToUpload">
      <input type="submit" id="submitFile" value="PostUsingAjax" ng-disabled="myForm.$invalid" ng-click="doFile(fileToUpload)">
    </form>
  </div>
</div>

I've added a little bit of the necessary wrapping. Here's a few things to take away:

1: Name the form so that you can refer to it in the following case ng-disabled="myForm.$invalid". The form was named myForm.

2: The required attribute on the input for the file does not need a value. Simply being present is enough.

3: I added a custom angularjs ng-click. It is possible to use the default onClick as you have, but may be bad to mix and match listeners.

4: Required doesn't work on file fields. You will need to add a directive. There are several other stack overflow answers regarding this.

Try any of the above answers.

vrintle
  • 5,501
  • 2
  • 16
  • 46
Dan Sabin
  • 896
  • 1
  • 9
  • 14
  • Thanks. I will try that. – user123 May 04 '16 at 17:12
  • It will [definitely](https://codepen.io/Rahul_V7/pen/pmGMMX) not work because angular-js currently don't support input[type='file']. Looking forward to a genuine answer. – vrintle Jun 03 '19 at 17:46