Skip to content

Commit 2617c0d

Browse files
author
Your Name
committed
Angular Forms In Depth
1 parent 74a52b1 commit 2617c0d

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/app/file-upload/file-upload.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</mat-form-field>
1212

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

1717
</button>

src/app/file-upload/file-upload.component.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,21 @@ export class FileUploadComponent implements ControlValueAccessor {
2121

2222
uploadProgress:number;
2323

24+
onChange = (fileName:string) => {};
25+
26+
onTouched = () => {};
27+
28+
disabled : boolean = false;
29+
2430
constructor(private http: HttpClient) {
2531

2632
}
2733

34+
onClick(fileUpload: HTMLInputElement) {
35+
this.onTouched();
36+
fileUpload.click();
37+
}
38+
2839
onFileSelected(event) {
2940

3041
const file:File = event.target.files[0];
@@ -56,6 +67,10 @@ export class FileUploadComponent implements ControlValueAccessor {
5667
if (event.type == HttpEventType.UploadProgress) {
5768
this.uploadProgress = Math.round(100 * (event.loaded / event.total));
5869
}
70+
else if (event.type == HttpEventType.Response) {
71+
this.onChange(this.fileName);
72+
73+
}
5974
});
6075

6176

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

6580
}
6681

82+
writeValue(value: any) {
83+
this.fileName = value;
84+
}
85+
86+
registerOnChange(onChange: any) {
87+
this.onChange = onChange;
88+
}
89+
90+
registerOnTouched(onTouched: any) {
91+
this.onTouched = onTouched;
92+
}
93+
94+
setDisabledState(disabled: boolean) {
95+
this.disabled = disabled;
96+
}
97+
98+
6799
}
68100

69101

0 commit comments

Comments
 (0)