[英]Angular: Disabling child control updating the parent form Pristine status
I have an angular form like this我有一个这样的 angular 表格
this.form = this._fb.group({
details: this._fb.group({
originId: [null, Validators.required],
... some more child controls
}),
... some more child controls
});
On ngOnInit()
when certain conditions fulfil I am marking the form dirty and touched, like this在
ngOnInit()
上,当某些条件满足时,我将表单标记为脏和触摸,就像这样
this.form.markAsDirty();
this.form.markAsTouched();
After this I get this.form.isPristine
as false
which is right, which is what I wanted.在此之后,我将
this.form.isPristine
为false
,这是正确的,这正是我想要的。
Then later I make control originId
disable like this to disable originId然后我像这样禁用
originId
来禁用 originId
this.form.get('details').get('originId').disable();
But the problem is, as soon as I disable the originId it updates the pristine status of the parent form to true
, so this this.form.isPristine
becomes true
which I do not want.但问题是,一旦我禁用 originId ,它就会将父表单的原始状态更新为
true
,所以这个this.form.isPristine
变成了我不想要的true
。
I am not getting why does setting the child control updates the parent form pristine status.我不明白为什么设置子控件会更新父窗体的原始状态。
Can anybody point out what is going wrong here?谁能指出这里出了什么问题?
The AbstractControl.disable
method propagates the disable flag to all direct ancestors as its default behavior which in your case results in your entire form being marked as disabled. AbstractControl.disable
方法将禁用标志作为其默认行为传播给所有直接祖先,在您的情况下,这会导致整个表单被标记为禁用。
see: https://angular.io/api/forms/AbstractControl#disable请参阅: https://angular.io/api/forms/AbstractControl#disable
To fix this, include the onlySelf: boolean
property in the disable options object.要解决此问题,请在禁用选项 object 中包含
onlySelf: boolean
属性。
this.form.get('details').get('originId').disable({onlySelf: true});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.