停止表单控件和表单组的值更改 [英] Stop Valuechanges of formcontrol and the formgroup

查看:22
本文介绍了停止表单控件和表单组的值更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单组,它有一个表单控件.我已经订阅了 formgroup 和 formcontrol 的 valuechanges 事件.通过单击按钮,我想禁用和重置 formcontrol 的值而不触发 valuechanges 所以我使用了 emitEvent:false 它不会触发 valuechangesformcontrol 但表单组的 valuechanges 被触发.我在这里有一个示例 plunker 演示 https://plnkr.co/edit/cN2wROc7o16w52ZEPZgH?p=preview.这是预期的行为还是问题.有人可以指导我

 ResetAndDisable(){this.ParentGroup.controls['test'].reset(null,{emitEvent:false});this.ParentGroup.controls['test'].disable({emitEvent:false});}使能够(){this.ParentGroup.controls['test'].enable({emitEvent:false});}

解决方案

您可以使用 emitEvent:falseonlySelf:true 的组合,其中 onlySelf:true ...<块引用>

如果 onlySelf 为 true,则此更改只会影响此 FormControl 的验证,而不影响其父组件.这默认为 false.

那么你可以做的是:

 ResetAndDisable(){this.ParentGroup.controls['test'].reset(null,{onlySelf:true,emitEvent:false});this.ParentGroup.controls['test'].disable({onl​​ySelf:true,emitEvent:false});}使能够(){this.ParentGroup.controls['test'].enable({onl​​ySelf:true,emitEvent:false});}

I have a formgroup which has a formcontrol. I have subscribed the valuechanges event of both formgroup and formcontrol.With a button click i would like to disable and reset value of formcontrol without firing valuechanges so i have used emitEvent:false which doesnot fire the valuechanges of the formcontrol but valuechanges of the formgroup is fired.I have a sample plunker demo here https://plnkr.co/edit/cN2wROc7o16w52ZEPZgH?p=preview .Is this expected behavior or an issue.Can somebody guide me

  ResetAndDisable(){
    this.ParentGroup.controls['test'].reset(null,{emitEvent:false});
    this.ParentGroup.controls['test'].disable({emitEvent:false});
 }
 Enable(){
    this.ParentGroup.controls['test'].enable({emitEvent:false});
 }

解决方案

You can use a combination of emitEvent:false and onlySelf:true, where onlySelf:true ...

If onlySelf is true, this change will only affect the validation of this FormControl and not its parent component. This defaults to false.

So what you can do is then:

    ResetAndDisable(){
      this.ParentGroup.controls['test'].reset(null,{onlySelf:true, emitEvent:false});
      this.ParentGroup.controls['test'].disable({onlySelf:true, emitEvent:false});
    }
    Enable(){
      this.ParentGroup.controls['test'].enable({onlySelf:true,  emitEvent:false});
    }

这篇关于停止表单控件和表单组的值更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆