valueChanges is still triggered even when using { emitEvent: false, onlySelf: true }

Discussão em 'Angular' iniciado por Yegor Petrov, Janeiro 10, 2025.

  1. Yegor Petrov

    Yegor Petrov Guest

    I would like to change value of FormControl in Angular 12. So I refered to this ticket and none of it helped: ValueChanges on FormControl triggers when Form.enable, even with emitEvent: false

    Here is a code:

    private subscribeToChanges(): void {
    const controlsToFollow = [
    'useDefaultBrightnessMode',
    'controlLedBrightnessDay',
    'controlLedBrightnessNight',
    'controlDisplayBrightnessDay',
    'controlDisplayBrightnessNight',
    'displayOffToExtendLifetime'
    ];

    controlsToFollow.forEach((controlName) => {
    const control = this.brightnessConfigurationForm.get(controlName) as FormControl;

    if (control) {
    this.asyncSaving.handleFormControlChanges(
    control,
    this.destroy$,
    () => this.saveBrightness()
    );
    }
    });
    }
    // I would like to set value to formControl without triggering valueChanges.
    private resetBrightness(): void {
    this.brightnessConfigurationForm.get('controlLedBrightnessDay')
    .setValue(this.defaultBrightnessDay, { emitEvent: false, onlySelf: true });
    this.brightnessConfigurationForm.get('controlLedBrightnessNight')
    .setValue(this.defaultBrightnessNight, { emitEvent: false, onlySelf: true });
    this.brightnessConfigurationForm.get('controlDisplayBrightnessDay')
    .setValue(this.defaultBrightnessDay, { emitEvent: false, onlySelf: true });
    this.brightnessConfigurationForm.get('controlDisplayBrightnessNight')
    .setValue(this.defaultBrightnessNight, { emitEvent: false, onlySelf: true });
    }


    There is my method from asyncSaving service:

    handleFormControlChanges<T>(
    formControl: FormControl,
    destroy$: Subject<void>,
    onValueChange: (value: T) => void,
    customOperators: Array<OperatorFunction<any, any>> = [tap(() => this.setLoading$(true))]): void {
    let composedChanges = formControl.valueChanges;
    for (const op of customOperators) {
    composedChanges = composedChanges.pipe(op);
    }

    composedChanges
    .pipe(
    takeUntil(destroy$),
    tap(() => this.setLoading$(true))
    )
    .subscribe(onValueChange);
    }


    Maybe there is someone that could help me out with this.

    Continue reading...

Compartilhe esta Página