Angular 2 提交后如何清除表单?

2024-02-18

我有一些带有模板的简单 Angular 2 组件。提交后如何清除表单和所有字段?我无法重新加载页面。 设置数据后date.setValue('')场依然touched.

import {Component} from 'angular2/core';
import {FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators, Control} from 'angular2/common';

@Component({
    selector: 'loading-form',
    templateUrl: 'app/loadings/loading-form.component.html',
    directives: [FORM_DIRECTIVES]
})

export class LoadingFormComponent {
    private form:ControlGroup;
    private date:Control;
    private capacity:Control;

    constructor(private _loadingsService:LoadingsService, fb:FormBuilder) {
        this.date = new Control('', Validators.required);
        this.capacity = new Control('', Validators.required);
        this.form = fb.group({
            'date': this.date,
            'capacity': this.capacity
        });
    }

    onSubmit(value:any):void {
        //send some data to backend
    }
}

加载表单.component.html

<div class="card card-block">
    <h3 class="card-title">Loading form</h3>

    <form (ngSubmit)="onSubmit(form.value)" [ngFormModel]="form">
        <fieldset class="form-group" [class.has-danger]="!date.valid && date.touched">
            <label class="form-control-label" for="dateInput">Date</label>
            <input type="text" class="form-control form-control-danger form-control-success" id="dateInput"
                   min="0" placeholder="Enter loading date"
                   [ngFormControl]="form.controls['date']">
        </fieldset>
        <fieldset class="form-group" [class.has-danger]="!capacity.valid && capacity.touched">
            <label class="form-control-label" for="capacityInput">Capacity</label>
            <input type="number" class="form-control form-control-danger form-control-success" id="capacityInput"
                   placeholder="Enter capacity"
                   [ngFormControl]="form.controls['capacity']">
        </fieldset>
        <button type="submit" class="btn btn-primary" [disabled]="!form.valid">Submit
        </button>
    </form>
</div>

也可以看看https://angular.io/docs/ts/latest/guide/reactive-forms.html https://angular.io/docs/ts/latest/guide/reactive-forms.html(“重置表单标志”部分)

>=RC.6

RC.6中应该支持更新表单模型。创建一个新的表单组并分配给myForm

[formGroup]="myForm"

也将得到支持(https://github.com/angular/angular/pull/11051#issuecomment-243483654 https://github.com/angular/angular/pull/11051#issuecomment-243483654)

>=RC.5

form.reset();

在新的表单模块中(>= RC.5)NgForm has a reset()方法,还支持表单reset event. https://github.com/angular/angular/blob/6fd5bc075d70879c487c0188f6cd5b148e72a4dd/modules/%40angular/forms/src/directives/ng_form.ts#L179 https://github.com/angular/angular/blob/6fd5bc075d70879c487c0188f6cd5b148e72a4dd/modules/%40angular/forms/src/directives/ng_form.ts#L179

这将起作用:

onSubmit(value:any):void {
  //send some data to backend
  for(var name in form.controls) {
    (<Control>form.controls[name]).updateValue('');
    /*(<FormControl>form.controls[name]).updateValue('');*/ this should work in RC4 if `Control` is not working, working same in my case
    form.controls[name].setErrors(null);
  }
}

也可以看看

  • https://github.com/angular/angular/issues/6196 https://github.com/angular/angular/issues/6196
  • https://github.com/angular/angular/issues/6169 https://github.com/angular/angular/issues/6169
  • https://github.com/angular/angular/issues/4933 https://github.com/angular/angular/issues/4933
  • https://github.com/angular/angular/issues/4914 https://github.com/angular/angular/issues/4914
  • https://github.com/angular/angular/issues/6371 https://github.com/angular/angular/issues/6371
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular 2 提交后如何清除表单? 的相关文章

随机推荐

  • 在 CSS 中将内部 div 与右下对齐

    将我的内部 div 放在右下角的最简单方法是什么 div style width 200px height 200px border 3px FF6666 solid div style font weight bold text alig
  • 使用 RxJS 从多个 API 调用构建数据

    我试图更好地了解如何使用 RxJS 运算符来解决我遇到的特定问题 我实际上有两个问题 但它们很相似 我正在从 API 端点获取一堆文档 api v3 folders folderId documents我已经设置了具有执行此操作的功能的服务
  • 按下按钮时 GPIO 事件检测不给出输出

    以下 python 脚本应该等待按钮按下 打印按钮按下消息 然后退出 但是 当我按下按钮时 什么也没有打印 然后 当我按回车键时 脚本会打印 检测到按钮按下 然后停止 我如何修复此代码 以便在按下按钮时打印 检测到按钮按下 我按照教程制作了
  • 使用Guava Cache将数据持久化到硬盘

    我是番石榴缓存的新手 如何在缓存中创建以下内容 像平常一样将数据保存在内存中 当缓存的大小超过给定的数字时 它将把缓存中的数据持久保存在硬盘上的可配置文件中 这可能会有所帮助 扩展 Guava 缓存以溢出到磁盘 http www javac
  • 将参数传递给 data.table 聚合函数

    我有一个函数可以计算变量的加权平均值 并使用以下方法按时间段对其进行分组data table聚合语法 但是 我想以编程方式提供加权列的名称 有没有办法在仍然使用传统方法的情况下实现这一目标data table句法 功能wtmean1下面演示
  • 应用程序在后台被杀死时的应用程序生命周期

    关于我遇到的问题的一些背景 在我的应用程序中 我有一个单例对象 我经常使用它来访问网络调用的 id 和令牌等内容 有时 当应用程序在后台被终止时 这个单例就会失去其状态 但是 当应用程序再次打开并在某些情况下启动时Activity经过发射器
  • 执行shell命令(c)

    这部分代码指示我的程序 生成屏幕截图 生成命令并退出 关闭 自身 这可用于使用程序中的键切换到程序 例如生成 gimp 或用户想要使用的另一个图像编辑器 case SWITCH TO if arg char commandline 256
  • Android 单元测试的最佳实践?

    我正在开发一个移动安卓应用程序 Android 单元测试最常用的库 框架有哪些 我感觉大部分业务逻辑 数据库测试 Web服务测试都可以使用JUnit来完成 但是 测试 UI UI 工作流程等的最佳方法是什么 例如 我们如何测试Android
  • Mechanize 无法在 Google Appengine 中自动登录 Gmail

    我已经使用 mechanize 并在 GAE 上部署了一个应用程序 它运行良好 但是 对于我正在制作的应用程序 我正在尝试通过 mechanize 自动登录 Gmail 它在本地计算机上的开发环境以及部署在 appengine 上后都不起作
  • 重新定义后更新变量值

    一个新手问题给各位 R 朋友 案例场景 我定义变量A A 数字 我根据A定义其他变量 B number A 我改变A的定义 A 不同的数字 问 如何让 R 自动更新 B 的值 而不需要再次重新定义它 例如 1 A 1000 2 B A 10
  • 如何在android中使用相机捕获自定义尺寸的图像?

    如何在android中捕捉方形图像 我想在android中通过intent调用Camera来捕获方形图像 例如300x300像素 我该怎么做 编辑 自 API 级别 21 起已弃用 使用 Camera Size 嵌套类 http devel
  • java.io.EOFException 与 paho

    我想对蚊子进行压力测试 所以我创建了一些代码 如下所示 for int i 0 i lt 800 i final int j i Thread t new Thread new Runnable Override public void r
  • 为什么 Sun Java 中的 HashSet 实现使用 HashMap 作为其支持?

    查看Java 6的源码 HashSet
  • 使用 SugarORM 和 GSON 解析字符串 id

    我在用着GSON创建一个SugarRecord来自 json 响应的对象 我使用的API返回一个名为 id 的字段 但 id 的类型是字符串 而不是long 后端使用mongo 下面是我正在使用的代码 Gson gson new Gson
  • 在 xaml 中创建字典?

    伪示例
  • 使用 JodaTime Instant 与 Hibernate 进行版本控制

    Using java time Instant与 Hibernate 一起工作 我正在使用hibernate java8 给出了更多的解决方案here https stackoverflow com a 26455446 581205 使用
  • 在 BLL 中处理异常并返回给客户端(winforms 或 webforms)?

    我正在寻找进行异常处理的最佳方法 例如 当业务逻辑层中发生错误时 使用 catch 停止方法并将事件返回到表示层的最佳方法是 这个活动应该包含什么内容 或者我应该总是冒泡异常并在表示层中处理它们 任何人都有一些很好的链接 并且需要阅读有关处
  • 在 SQLite 中转义表名?

    我在 SQLite 中有一个名为 References 的表 所以我似乎无法定位它 我用 SQLite studio 编辑数据库抛出错误 有没有办法转义数据库名称 查询是 UPDATE References SET DateTimeLast
  • 如何在 C# 中创建动态设置属性名称的 JSON 对象?

    我需要返回一个 JSON 对象数组 它是 SQL 查询的结果 SQL 查询可以是任何内容 因此我需要在代码中动态创建属性名称和值 例如 从员工中选择first name last name 我想返回 data first name dave
  • Angular 2 提交后如何清除表单?

    我有一些带有模板的简单 Angular 2 组件 提交后如何清除表单和所有字段 我无法重新加载页面 设置数据后date setValue 场依然touched import Component from angular2 core impo