【发布时间】:2018-07-30 01:56:30
【问题描述】:
我正在使用 Angular 6,并且我有一个包含很多字段的反应形式。有些是下拉菜单,有些是数字,默认情况下它们的值为空,而其他一些下拉菜单的默认值默认为true。
用户可以选择字段的一个或全部或任意组合。所以我不能拥有所有需要的字段。
但是我必须知道表单是否还有它的默认值,所以我可以取消提交。
我尝试了一些检查
ngOnInit() {
this.initialState = this.form.value;
}
submit(){
if(this.initialState != this.form.value)...
}
报错
submit(){
if(this.form.pristine)...
}
表单仍然可以改回其默认值
我可以使用某种 for ,但我有 8 个字段,因此在每次提交尝试之前循环 8 次并不理想。
所以我必须手动检查 if 之类的
if (!this.form.control.field1.value && this.form.control.field2.value === true ....)??
有没有更快更好的方法?只检查整个表格?
谢谢
【问题讨论】:
-
提交后为什么不重置表单并设置默认值
-
this.initialState != this.form.value 每次都会给出 false , object test by refrence 和 form.value defrreant object
-
提交前怎么办?
-
你可以使用 form.reset({name:'value'}) 所以提交后重置值
-
对不起,我不明白你所说的
this.initialState != this.form.value等。你能进一步解释一下吗?
标签: angular forms angular6 angular-reactive-forms