简体   繁体   English

提交后如何清除输入(重置表单)

[英]How to clear inputs (reset forms) after submitting

I am starting to program angular and I do not understand how I can clear the inputs after clicking a submit button.我开始对 angular 进行编程,但我不明白单击提交按钮后如何清除输入。

Can anyone explain to me a simple procedure of being able to clear / reset the inputs / forms after clicking the submit button?任何人都可以向我解释一个能够在单击提交按钮后清除/重置输入/表单的简单程序吗? Is there a way to put any validator in one of the fields?有没有办法在其中一个字段中放置任何验证器?

I've seen some topics on the subject, but could not understand the logic.我看过有关该主题的一些主题,但无法理解其中的逻辑。

DEMO 演示

HTML HTML

<form>
  <div class="form-group">
    <label for="exampleFormControlInput1">Email address</label>
    <input type="email" class="form-control" id="exampleFormControlInput1">
  </div>
   <div class="form-group">
    <label>Adress</label>
    <input type="text" class="form-control" id="exampleFormControlInput1">
  </div>
   <div class="form-group">
    <label>Name</label>
    <input type="text" class="form-control" id="exampleFormControlInput1">
  </div>
  <div class="form-group">
    <label for="exampleFormControlTextarea1">Example textarea</label>
    <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
  </div>
  <button type="submit" (click)="clearforms()">SUBMIT</button>
</form>

I'd strongly recommend following the first commenter's advice.我强烈建议遵循第一个评论者的建议。

Here is a working example which makes use of Template Driven Forms , which basically represents a declarative way to build your form tree. 是一个使用Template Driven Forms的工作示例,它基本上代表了一种构建表单树的声明方式。

If you're dealing with more dynamic stuff, you should consider using Reactive Forms , it will give you much more control on the situation.如果你正在处理更多动态的东西,你应该考虑使用Reactive Forms ,它会让你更好地控制情况。

You can also provide validators.您还可以提供验证器。 You can do this either through some directives, or, especially when using Reactive Forms, through the Validators object.你可以通过一些指令来做到这一点,或者,尤其是在使用 Reactive Forms 时,通过Validators对象。

For instance, here's how you'd use an email validator as a directive:例如,以下是您将电子邮件验证器用作指令的方式:

<input ngModel name="email" email>

Here is a file that contains all the validators which are available as directives. 是一个包含所有可用作指令的验证器的文件。

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

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