简体   繁体   中英

The mat spinner doesn't hide after changing the control property

<mat-card *ngIf="isLoading" style="display: flex; justify-content: center; align-items: center">
  <mat-progress-spinner color="primary" mode="indeterminate"></mat-progress-spinner>
</mat-card>

loadData(filters: MultipleExpenseFilterRequest) {
    this.isLoading = true;
    this.taskService.getAllTasks().subscribe(taskData => {
      this.taskList = taskData;
      this.expensesService.getAllExpensesTypes().subscribe(expensesTypeData => {
        this.expensesTypeList = expensesTypeData;
        this.expensesService.getAllCurrencyType().subscribe(currencyData => {
          this.currencyList = currencyData;
          this.initializeExpenses();
        });
      });
    });
  }

  initializeExpenses() {
    this.expensesService.getAllExpenses(11, 2019).subscribe(data => {
      this.isLoading = false;
      this.expenses.data = data;
    });
  }

Changing the value of the control property for the mat-spinner (isLoading) doesn't hide the mat-spinner. The spinner is hidden only when clicking or moving the mouse over it.

This happens when the datasource.data of the table that I am using is empty. So I get no data from the server.

Try *ngIf="isLoading == true" to specify the state of the boolean. *ngIf="isLoading" would refer to anything as long as the boolean is defined.

Assign false to isLoading if error happens to the API, and also use ChangeDetectorRef :

constructor(private cd: ChangeDetectorRef) {
}

initializeExpenses() {
    this.expensesService.getAllExpenses(11, 2019).subscribe(data => {
      this.isLoading = false;
      this.expenses.data = data;
      this.cd.detectChanges();
    }, error => {
       this.isLoading = false;
       this.cd.detectChanges();
    });
}

You also need to use the error part for the first three API calls. The spinner would stay if the error happens because it does not reach the initializeExpenses() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM