Open
Description
Currently all the form controls have implemented the ControlValueAccessor
. While if the users wants to wrap the form controls themselves, it will be quite redundant. They have to implement the ControlValueAccessor all over again.
In here, the way is much more easier thanks to the ngDefaultControl
which Angular exposed.
@Component({
selector: 'my-wrapper',
template: '<input ngDefaultControl>',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => NumberInputComponent),
multi: true,
},
],
})
export class MyWrapper implements ControlValueAccessor {
@ViewChild(DefaultValueAccessor) private valueAccessor: DefaultValueAccessor;
writeValue(obj: any) {
this.valueAccessor.writeValue(obj);
}
registerOnChange(fn: any) {
this.valueAccessor.registerOnChange(fn);
}
registerOnTouched(fn: any) {
this.valueAccessor.registerOnTouched(fn);
}
setDisabledState(isDisabled: boolean) {
this.valueAccessor.setDisabledState(isDisabled);
}
}
The way doesn't work in the nativescript-angular because the ngDefaultControl
is not exposed in here.
By simply put the ngDefaultControl
selector in all the value-accessor. We can implement the same feature.