简体   繁体   English

Angular:禁用子控件更新父窗体 Pristine 状态

[英]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 thisngOnInit()上,当某些条件满足时,我将表单标记为脏和触摸,就像这样

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.isPristinefalse ,这是正确的,这正是我想要的。

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.

相关问题 Angular form reset() 不会恢复到原始状态 - Angular form reset() does not return to pristine status 在反应角形式中观察形式的原始状态时出现问题 - Problem when watching for pristine status of form in reactive angular form Angular:指令内未获取表格原始/无效状态的引用 - Angular: not getting reference of Form pristine/invalid status inside directive 即使原始,Angular Material窗体控件也以红色突出显示 - Angular Material form control highlighted in red even though pristine Angular 反应形式验证子控件中的所需状态 - Angular reactive form validate required status in child control 在子组件Angular 5控制值访问器中访问父表单验证 - Access parent form validation in child component Angular 5 control value accessor Angular子窗体控件/组值和错误冒泡至父级 - Angular child form control/group values and errors bubbling up to parent 角父窗体控件Bootstrap / CSS不适用于子组件 - Angular parent form-control Bootstrap / CSS is not applied to child component Angular 9 听表原始变化 - Angular 9 listen to form pristine change Angular - 当无效表单绑定到也是一个角度组件的子表单控制器时,父表单不会更新 - Angular - Parent form not updating, when invalid form is bound to the child form controller which is also an angular component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM