最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use AngularJS $submitted? - Stack Overflow

programmeradmin1浏览0评论

On submit, AngularJS sets $submitted on the FormController and adds the class ng-submitted to the form. That's nice, and I could add

ng-disabled="myForm.$submitted || myForm.$invalid || maybeAnotherCondition"

to the submit button, and disable all inputs (as there's no point in re-submitting or editing anything before the call returns). The inputs should probably be re-enabled as soon as the call returns and the submit button on the first following input change...

Quite a lot of things to do and before I start with it, I'd like to know, if there's already some pattern or even a directive doing all this stuff? I could imagine having something like

FormService.manage(form, onSubmit, onSuccess, onFailure)

where each of the three functions would do just the specific job and nothing of the above boilerplate.

On submit, AngularJS sets $submitted on the FormController and adds the class ng-submitted to the form. That's nice, and I could add

ng-disabled="myForm.$submitted || myForm.$invalid || maybeAnotherCondition"

to the submit button, and disable all inputs (as there's no point in re-submitting or editing anything before the call returns). The inputs should probably be re-enabled as soon as the call returns and the submit button on the first following input change...

Quite a lot of things to do and before I start with it, I'd like to know, if there's already some pattern or even a directive doing all this stuff? I could imagine having something like

FormService.manage(form, onSubmit, onSuccess, onFailure)

where each of the three functions would do just the specific job and nothing of the above boilerplate.

Share Improve this question edited May 28, 2016 at 3:42 Shashank Agrawal 25.8k11 gold badges96 silver badges125 bronze badges asked May 28, 2016 at 2:05 maaartinusmaaartinus 46.5k40 gold badges175 silver badges340 bronze badges 3
  • Were you able to disable a single input field and now want to disable all the input fields? – Shashank Agrawal Commented May 28, 2016 at 3:31
  • @ShashankAgrawal I did nothing yet as I'm looking for a simple solution to be applied to all my forms. I don't want to pollute all inputs with ng-disabled. Using the fieldset is helpful, but I'm looking for more. – maaartinus Commented May 28, 2016 at 15:19
  • 1 Alternatively, you can write a directive that will do things for you like I did in one of my directive github./sagrawal14/bootstrap-angular-validation/blob/master/… to apply global Bootstrap validation like jQuery in my many forms without writing anything extra. – Shashank Agrawal Commented May 29, 2016 at 5:17
Add a ment  | 

1 Answer 1

Reset to default 4

If you want to disable all the input fields at once, I would suggest you use <fieldset>.

<form name="myForm" id="myForm" ng-submit="someHandler()">
    <fieldset form="myForm" ng-disabled="myForm.$submitted || myForm.$invalid || maybeAnotherCondition">
        <label>First Name</label>
        <input type="text" name="firstName" data-ng-model="firstName" />

        <label>Last Name</label>
        <input type="text" name="lastName" data-ng-model="lastName" />

        <button type="submit">Submit</button>
    </fieldset>
</form>

Disabling the fieldset element will disable all it's child input elements. See a working example below:

var app = angular.module("sa", []);

app.controller("FooController", function($scope) {

  $scope.submitHandler = function() {
    alert("Form submitted");
  }
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.3.7/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">


<div ng-app="sa" ng-controller="FooController" class="container">
  <form name="myForm" id="myForm" ng-submit="submitHandler()">
    <fieldset form="myForm" ng-disabled="myForm.$submitted">
      <div class="form-group">
        <label>First Name</label>
        <input type="text" name="firstName" data-ng-model="firstName" class="form-control" />
      </div>

      <div class="form-group">
        <label>Last Name</label>
        <input type="text" name="lastName" data-ng-model="lastName" class="form-control" />
      </div>

      <button type="submit" class="btn btn-primary">
        {{myForm.$submitted ? 'Submitted' : 'Submit'}}
      </button>
    </fieldset>
  </form>
</div>

发布评论

评论列表(0)

  1. 暂无评论