Skip to content

Instantly share code, notes, and snippets.

@sheikalthaf
Created March 12, 2019 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheikalthaf/85c19d41bccf218d6bc962daa75a7943 to your computer and use it in GitHub Desktop.
Save sheikalthaf/85c19d41bccf218d6bc962daa75a7943 to your computer and use it in GitHub Desktop.
file value handling in angular forms
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