Вот код:
Код: Выделить всё
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 });
}
Код: Выделить всё
handleFormControlChanges(
formControl: FormControl,
destroy$: Subject,
onValueChange: (value: T) => void,
customOperators: Array = [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);
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... nlyself-tr
Мобильная версия