角度:禁用儿童控制更新父级原始状态
我在ngoninit()
上有类似的角色形式
this.form = this._fb.group({
details: this._fb.group({
originId: [null, Validators.required],
... some more child controls
}),
... some more child controls
});
,当某些条件满足时,我在标记肮脏的形式并触摸了,这样
this.form.markAsDirty();
this.form.markAsTouched();
,我得到this.form.ispristine
as false
是正确的,这是我想要的。
然后稍后,我将Control OriginID
这样禁用以禁用OriginID,
this.form.get('details').get('originId').disable();
但是问题是,一旦我禁用OriginID,就会将父级的原始状态更新为true> True
,因此,这个this.form.ispristine
变成 true ,我不想要。
我没有得到为什么设置子控制会更新父级原始状态。
有人可以指出这里出了什么问题吗?
I have an angular form like this
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
this.form.markAsDirty();
this.form.markAsTouched();
After this I get this.form.isPristine
as false
which is right, which is what I wanted.
Then later I make control originId
disable like this to disable 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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AbstractControl.disable
方法将禁用标志传播给所有直接祖先,因为在您的情况下,这会导致整个形式被标记为残疾人。请参阅: https://angular.io/api/api/api/api/api/api/aptart/aptart/abstractcontrort contrort controll #disable 修复此操作,包括
selfs:boolean
在禁用选项对象中的属性。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.see: https://angular.io/api/forms/AbstractControl#disable
To fix this, include the
onlySelf: boolean
property in the disable options object.