Having trouble displaying a loading spinner while a function is running. I've tried different methods, but the spinner just won't appear.
Here's the HTML snippet:
<div class="row pt-3" id="firstRow">
<div class="col">
<button class="btn btn-dark back-button">
<fa name="upload"></fa>
<span>Load .csv file</span>
</button>
<input type="file" #fileImportInput name="File Upload" class="txtFileUpload p-3" (change)="fileChangeListener($event)" accept=".csv" />
<img class="spinner" *ngIf="loading" src="../../../assets/img/gif-spinner/Spin-2s-200px.gif" />
</div>
</div>
This is part of the TypeScript code:
export class MotionAnalysisComponent implements OnInit {
loading = false;
fileChangeListener($event: any): void {
this.loading = true;
let files = $event.srcElement.files;
if (this.isCSVFile(files[0])) {
let input = $event.target;
let reader = new FileReader();
reader.readAsText(input.files[0]);
reader.onload = () => {
let csvData = reader.result;
let csvRecordsArray = (<string>csvData).split(/\r\n|\n/);
this.csvRecords = this.getDataRecordsArrayFromCSVFile(csvRecordsArray, 4);
};
reader.onerror = function () {
alert('Unable to read ' + input.files[0]);
};
} else {
alert("Please import valid .csv file.");
this.fileReset();
}
this.loading = false;
If I remove the "this.loading = false" line within the function, the spinner appears after the function has finished executing and stays visible. How can I instruct the HTML to display the spinner during the function execution?