-1

I have an icon "fa-duotone fa-rotate-right", and I want when changing "loading = true", the class "fa-spin" is added, everything works ok, but when it becomes "false" it does not stop, I’m a beginner in Angular, don’t discuss much, maybe you know how you can decide or tell me. Thanks

Html

 <button mat-fab color="primary" class="float-end"  (click)="load()">
     <mat-icon class="fa-duotone fa-rotate-right" [ngClass]="loading  ? 'fa-spin' : ''"></mat-icon>
 </button>

Typescript

  load(): void {
    this.loading = true;
    this.productsService.getProducts().subscribe(
      (response: any) => {
        this.dataSource = new MatTableDataSource(response.products);
        this.dataSource.paginator = this.paginator;
        this.loading = false;
        this.selection.clear();
      },
      error => {
        this.alertService.error('Server error in Getting Products');
      }
    );
  }
1

2 Answers 2

0

Your syntax for ngClass is not correct. Try this:

<mat-icon 
  class="fa-duotone fa-rotate-right"
  [ngClass]="{'fa-spin': loading}"
>
</mat-icon>
Sign up to request clarification or add additional context in comments.

7 Comments

It does not work, the icon continues to spin (animation), that is, at first it is true, then when it becomes false it should stop, but the animation continues to work anyway.
Are you completely sure that you are getting success response from the server? Maybe you are getting error, and you don't set loading to false in the error handler.
I need fa-spin to work somehow asynchronously, at first it is always true, but after a few seconds when I get a response from the back-end, it becomes false, but the class inside the icon still remains
There are no errors, since I receive products from the backend and successfully show in the table
Wait.... I am not sure, but can you try to remove fa-rotate-right class? Maybe that class is also adding spinner. And that class is always present.
|
0

In your exemple, the sintax of the [ngClass] it's wrong. Change to this:

<button mat-fab color="primary" class="float-end" (click)="load()">
    <mat-icon class="fa-duotone fa-rotate-right" [ngClass]="{'fa-spin': loading}"></mat-icon>
</button>

I did this change and is working, probly your problem is that the your query returning a error, and than try set "loading = false" inside error code:

 load(): void {
    this.loading = true;
    this.productsService.getProducts().subscribe(
      (response: any) => {
        this.dataSource = new MatTableDataSource(response.products);
        this.dataSource.paginator = this.paginator;
        this.loading = false;
        this.selection.clear();
      },
      error => {
        this.loading = false;
        this.alertService.error('Server error in Getting Products');
      }
    );
  }

Good luck :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.