AngularJS: disabling all form controls between submit and server response

前端 未结 2 1246
我寻月下人不归
我寻月下人不归 2020-12-04 07:06

I have a dilemma about what is the best (and correct) approach if I want to disable form controls (or at least make them unavailable for user interaction) during a period of

相关标签:
2条回答
  • 2020-12-04 07:41

    Wrap all your fields in fieldset and use ngDisabled directive like this:

    <fieldset ng-disabled="isSaving"> ... inputs ...</fieldset>
    

    It will automatically disable all inputs inside the fieldset.

    Then in controller set $scope.isSaving to true before http call and to false after.

    0 讨论(0)
  • 2020-12-04 07:54

    There is an simple solution in modern browsers:

    1. define a css class

      .disabled {
        pointer-events: none;
        ... ...
      }
      
    2. add this class to ng-form

      <ng-form data-ng-class="{ 'disabled': isSaving }"> ... inputs ... </ng-form>
      

    Here is the pointer-events support chart.

    Note: even if you set pointer-events: none, you can still tab to input element with your keyboard.

    0 讨论(0)
提交回复
热议问题