Created
March 12, 2019 10:18
-
-
Save sheikalthaf/85c19d41bccf218d6bc962daa75a7943 to your computer and use it in GitHub Desktop.
file value handling in angular forms
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 { Directive, HostListener, Output, EventEmitter } from '@angular/core'; | |
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; | |
@Directive({ | |
// tslint:disable-next-line:directive-selector | |
selector: 'input[type="file"][ngu-file-ref]', | |
exportAs: 'nguFiles', | |
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: FileValueDirective, multi: true }] | |
}) | |
// tslint:disable-next-line:class-name | |
export class FileValueDirective implements ControlValueAccessor { | |
onChange: (_: any) => {}; | |
@HostListener('blur', ['$event']) | |
onTouched: () => {}; | |
@HostListener('change', ['$event.target.files']) | |
fileChanges(evt: File) { | |
this.onChange(evt); | |
} | |
writeValue(value: any): void { | |
} | |
registerOnChange(fn: any) { | |
this.onChange = fn; | |
} | |
registerOnTouched(fn: any): void { | |
this.onTouched = fn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment