Skip to content

Instantly share code, notes, and snippets.

@afirdousi
Last active July 2, 2017 03:36
Show Gist options
  • Save afirdousi/6bde85fd0bb56b5a4598373abf097a65 to your computer and use it in GitHub Desktop.
Save afirdousi/6bde85fd0bb56b5a4598373abf097a65 to your computer and use it in GitHub Desktop.
import { Component, Input, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
const customValueProvider = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CustomInput),
multi: true
};
@Component({
selector: 'custom-input',
template:`
<div class="custom-input">
{{ label }}
<input type="text" (input)="onChange($event)" />
</div>
`
providers: [ {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CustomInput),
multi: true
} ]
})
export class CustomInput implements ControlValueAccessor {
_value = '';
propagateChange:any = () => {};
@Input() label: string;
writeValue(value: any) {
if( value ){
this._value = value;
}
}
registerOnChange(fn) {
this.propagateChange = fn;
}
registerOnTouched(fn: () => void): void { }
onChange(event){
this.propagateChange(event.target.value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment