[Fixed] Stop Valuechanges of formcontrol and the formgroup

Issue

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});
 }

Solution

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});
    }

Leave a Reply

(*) Required, Your email will not be published