简体   繁体   English

如何使用AngularJS $提交?

[英]How to use AngularJS $submitted?

On submit, AngularJS sets $submitted on the FormController and adds the class ng-submitted to the form. 在提交,AngularJS集$submittedFormController并添加类ng-submitted到窗体。 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. 其中三个函数中的每一个都只执行特定的工作而不执行上述样板操作。

If you want to disable all the input fields at once, I would suggest you use <fieldset> . 如果你想一次禁用所有输入字段,我建议你使用<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. 禁用fieldset元素将禁用其所有子输入元素。 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.com/ajax/libs/angularjs/1.3.7/angular.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM