在 angular 2 中重置模板驱动的表单(模型和验证) [英] Resetting template driven forms in angular 2 (models and validations both)

查看:30
本文介绍了在 angular 2 中重置模板驱动的表单(模型和验证)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何使用 angular 2 中模板驱动表单的验证状态重置控件?我知道可以通过设置它们绑定到的模型值来重置控件.但是验证状态(原始、脏等)呢?

How do we reset the controls with validation states of Template driven forms in angular 2? I know that controls can be reset by setting model values to which they are bound to. But what about the validation states(pristine, dirty etc..)?

我尝试过这样的事情:

<form (ngSubmit)="onSubmit(playlistForm)" #playlistForm="ngForm">
// Some code here...
</form>

在控制器中,

onSubmit(playlistForm: any) {
// ...
  playlistForm.form.reset();
}

但在上面似乎实际上重定向到了 ''我在下面收到错误:

But in above seems to actually redirect to the '' And I get error below:

EXCEPTION: Error: Uncaught (in promise): Error: Cannot match any routes: '' 

那么我该怎么做我想做的事?

So how do I do what I want?

推荐答案

这两个都对我有用:

playlistForm.reset();
playlistForm.resetForm(); // I think this is the one to use

Angular 文档只有 resetForm()https://angular.io/docs/ts/latest/api/forms/index/NgForm-directive.html

Angular docs have resetForm() only https://angular.io/docs/ts/latest/api/forms/index/NgForm-directive.html

这篇关于在 angular 2 中重置模板驱动的表单(模型和验证)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆