重置角度2中的模板驱动形式(模型和验证均使用) [英] Resetting template driven forms in angular 2 (models and validations both)
本文介绍了重置角度2中的模板驱动形式(模型和验证均使用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用角度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: ''
那我该怎么做?
推荐答案
这两种方法都对我有用:
Both of these worked for me:
playlistForm.reset();
playlistForm.resetForm(); // I think this is the one to use
角度文档仅具有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
这篇关于重置角度2中的模板驱动形式(模型和验证均使用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文