NgxControlValueAccessor
NgxControlValueAccessor
is a directive to reduce boilerplate when building components, which implement the ControlValueAccessor interface.
Import
Usage
NgxControlValueAccessor
implements the ControlValueAccessor interface and exposes a simpler api. Declare NgxControlValueAccessor
in the hostDirectives
section of your component and inject the instance in order to wire up your template:
NgxControlValueAccessor.value
for syncing the valueNgxControlValueAccessor.disabled
for syncing the disabled stateNgxControlValueAccessor.markAsTouched
for marking the view as touched
The value and disabled state are also available as signals:
NgxControlValueAccessor.value$
NgxControlValueAccessor.disabled$
Example
In this example NgxControlValueAccessor
is used to create a CustomInput
component.
With usage:
Non Primitive Values
When your model is a non primitive datatype, you should provide a comparator. It is a pure function which tells NgxControlValueAccessor
, whether two values are semantically equal:
Example
In this example NgxControlValueAccessor
is used to create a User
select. A User
is identified by its id
.
Full example:
With usage:
Without NgControl
Optionally you can expose inputs
and outputs
in the hostDirectives
declaration
and use it without a NgControl
directive.