Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire the `valueChanges` programmatically?

On one of my pages I use a FormBuilder to fill a form at initialization. Every input gets a class whenever the input is not empty. This is done by adding a ngClass to every input, and subscribing on the FormGroup's valueChanges.

My problem occurs whenever the form is filled programmatically. Whenever a user changes any value on the form, valueChanges gets called, however, this is not the case when using a FormBuilder.

My question is: How to get the valueChanges event to be fired whenever the FormBuilder is finished.

I have tried using this.FormGroup.updateValueAndValidity(), but this did not result in anything.

like image 468
Max Avatar asked Feb 24 '17 10:02

Max


1 Answers

I found a solution which worked for me. I forgot two parameters in the updateValueAndValidity function.

  • onlySelf: will only update this FormControl when true.
  • emitEvent: will cause valueChanges event to be fired when true.

So as result you will get something like: FormGroup.updateValueAndValidity({ onlySelf: false, emitEvent: true });

like image 160
Max Avatar answered Sep 28 '22 06:09

Max