Skip to content

Commit

Permalink
Angular Forms In Depth
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jan 15, 2021
1 parent 74a52b1 commit 2617c0d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/app/file-upload/file-upload.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</mat-form-field>

<button mat-mini-fab color="primary" class="upload-btn"
(click)="fileUpload.click()">
(click)="onClick(fileUpload)" [disabled]="disabled">
<mat-icon>attach_file</mat-icon>

</button>
Expand Down
32 changes: 32 additions & 0 deletions src/app/file-upload/file-upload.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,21 @@ export class FileUploadComponent implements ControlValueAccessor {

uploadProgress:number;

onChange = (fileName:string) => {};

onTouched = () => {};

disabled : boolean = false;

constructor(private http: HttpClient) {

}

onClick(fileUpload: HTMLInputElement) {
this.onTouched();
fileUpload.click();
}

onFileSelected(event) {

const file:File = event.target.files[0];
Expand Down Expand Up @@ -56,6 +67,10 @@ export class FileUploadComponent implements ControlValueAccessor {
if (event.type == HttpEventType.UploadProgress) {
this.uploadProgress = Math.round(100 * (event.loaded / event.total));
}
else if (event.type == HttpEventType.Response) {
this.onChange(this.fileName);

}
});


Expand All @@ -64,6 +79,23 @@ export class FileUploadComponent implements ControlValueAccessor {

}

writeValue(value: any) {
this.fileName = value;
}

registerOnChange(onChange: any) {
this.onChange = onChange;
}

registerOnTouched(onTouched: any) {
this.onTouched = onTouched;
}

setDisabledState(disabled: boolean) {
this.disabled = disabled;
}


}


Expand Down

0 comments on commit 2617c0d

Please sign in to comment.