2

I need to create a loading component with Angular 11. When loading data, for example, I want my loading-component to be displayed otherwise other components should work.

I don't have a proper idea to do this?

Can anyone help me?

1 Answer 1

3

you can create fullscreen loading overlay in app component and to show and hide that component use service and observable

app component: html:

<div *ngIf="loadingService.isLoading$ | async">Loading...</div>

ts:

constructor(public loadingService: LoadingService) {

}

loading service:

class LoadingService {
  private isLoading = new Subject<boolean>();
  isLoading$ = this.isLoading as Observable;

  toggleLoading(val: boolean) {
    this.isLoading.next(val);
  }
}

to start loading from any component inject service and call toggle loading method:

constructor(private loadingService: LoadingService) {
  this.loadingService.toggleLoading(true);
}

there can be some syntax errors or typos in this code as i have not tested it, just sharing the idea let me know if you face any other problem

Sign up to request clarification or add additional context in comments.

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.