角度:禁用儿童控制更新父级原始状态

发布于 2025-01-23 22:39:13 字数 788 浏览 1 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风向决定发型 2025-01-30 22:39:13

AbstractControl.disable方法将禁用标志传播给所有直接祖先,因为在您的情况下,这会导致整个形式被标记为残疾人。

请参阅: https://angular.io/api/api/api/api/api/api/aptart/aptart/abstractcontrort contrort controll #disable 修复此操作,包括selfs:boolean在禁用选项对象中的属性。

this.form.get('details').get('originId').disable({onlySelf: true});

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.

this.form.get('details').get('originId').disable({onlySelf: true});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文