RangeError: Maximum call stack size exceeded when using valueChanges.subscribe

后端 未结 4 764
南方客
南方客 2021-02-02 08:30

I am using Angular 5 with Reactive forms and need to make use of the valueChanges in order to disable required validation dynamically

component class:

ex         


        
4条回答
  •  误落风尘
    2021-02-02 09:20

    If you want to subscribe to any form changes and still run patchValue inside it, then you could add the {emitEvent: false} option to patchValue, thus the patching will not trigger another change detection

    code:

    this.formGroup
        .valueChanges
        .subscribe( _ => {
            this.formGroup.get( 'controlName' ).patchValue( _val, {emitEvent: false} );
        } );
    

    PS. This is also less tedious than subscribing to each form control one-by-one to avoid triggering change max call stack exceeded. Especially if you form has 100 controls to subscribe to.

    Now to elaborate further, if you still need to updateValueAndValidity inside the subscription, then I suggest you use the distinctUntilChanged rxjs operator, to only run the subscription, when some value changes.

    distinctUntilChanged documentation can be found here

    https://www.learnrxjs.io/operators/filtering/distinctuntilchanged.html

    distinctUntilChanged - Only emit when the current value is different than the last.

    Now we will also have to make it a custom validation function, because by default, distinctUntilChanged validates objects by pointer and the pointer is new on every change.

    this.formGroup
        .valueChanges
        .distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b))
        .subscribe( _ => {
            this.formGroup.get( 'controlName' ).patchValue( _val, {emitEvent: false} );
            this.formGroup.get( 'controlName' ).updateValueAndValidity();
        } );
    

    And voila, we are patching and updating, without running into the maximum call stack!

提交回复
热议问题