admin管理员组文章数量:1123379
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.
本文标签:
版权声明:本文标题:javascript - valueChanges is still triggered even when using { emitEvent: false, onlySelf: true } - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736566403a1944710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论