Skip to content

Instantly share code, notes, and snippets.

@Stradivario
Last active March 14, 2022 17:14
Show Gist options
  • Save Stradivario/eefb4deb844511ff9b918afe2a93bc26 to your computer and use it in GitHub Desktop.
Save Stradivario/eefb4deb844511ff9b918afe2a93bc26 to your computer and use it in GitHub Desktop.
/* eslint-disable @typescript-eslint/no-unused-vars */
import {EventEmitter, forwardRef} from '@angular/core';
import {
ControlValueAccessor as Control,
NG_VALUE_ACCESSOR,
} from '@angular/forms';
export const VALUE_ACCESSOR = <T>(clazz: T) => ({
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => clazz),
multi: true,
});
export class ControlValueAccessor<T = any> implements Control {
value?: T;
valueChange: EventEmitter<T>;
onChange = (value: T) => undefined;
onTouched = (value: T) => undefined;
writeValue(value: T): void {
this.value = value || this.value;
}
pushChanges(value: T) {
this.onChange(value);
}
registerOnChange(fn: (value: T) => undefined): void {
this.onChange = fn;
}
registerOnTouched(fn: (value: T) => undefined): void {
this.onTouched = fn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment