Last active
July 2, 2017 03:36
-
-
Save afirdousi/6bde85fd0bb56b5a4598373abf097a65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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