feat(NgxControlValueAccessor): Add NgxControlValueAccessor #227
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
NgxControlValueAccessor
is a directive to reduce boilerplate when building components, which implement the ControlValueAccessor interface.Usage
NgxControlValueAccessor
implements the ControlValueAccessor interface and exposes a simpler api. DeclareNgxControlValueAccessor
in thehostDirectives
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 touchedThe value and disabled state are also available as signals:
NgxControlValueAccessor.value$
NgxControlValueAccessor.disabled$
Example
In this example
NgxControlValueAccessor
is used to create aCustomInput
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 aUser
select. AUser
is identified by itsid
.Full example:
With usage:
Without
NgControl
Optionally you can expose
inputs
andoutputs
in thehostDirectives
declarationand use it without a
NgControl
directive.