Danaley
Danaley

Reputation: 803

How can I listen to valueChanges and setValue of a AbstractControl in reactive forms

I am new to Angular and I have a Reactive Forms with AbstractControl that subscribe for valueChange to listen for any values change in input field. Inside the subscribed function I want to change the value of the input under a certain condition.

this.abstractControl.valueChanges.subscribe(data => {
...
   if(.....) {
       this.formGroup.patchValue({
           name: result
       )}
    }
...
}

I tried to use this.abstractControl.setValue(result) does'nt work too.

I get an error in setting the value.

ERROR RangeError: Maximum call stack size exceeded

What is the recommended way of listening to a value change and changing the value accord to a condtion?

Upvotes: 6

Views: 13223

Answers (1)

Danaley
Danaley

Reputation: 803

@jonrsharpe thanks. Problem resolved with emitEvent: false.

Read: angular.io/api/forms/FormControl#setValue

Upvotes: 15

Related Questions