Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to laod this with every Rest call #52

Open
rrohitesh opened this issue Jun 21, 2017 · 3 comments
Open

How to laod this with every Rest call #52

rrohitesh opened this issue Jun 21, 2017 · 3 comments

Comments

@rrohitesh
Copy link

Hello,

Can you please provide information on how to load this loading bar with rest call so that when i load any component it should ?

@NGorco
Copy link

NGorco commented Jun 26, 2017

You should create custom Http service (actually, a wrapper around basic Http) and use it everywhere. On each put/get/post etc fire progress bar loading.

@angelayanpan
Copy link

In the most recent release of Angular 4.3, there is a functionality called HTTP interceptor. And you can use it for HTTP call progress tracking. Here is a link to the official doc: https://angular.io/guide/http#intercepting-all-requests-or-responses

@stuartaccent
Copy link

This may or may not be perfect but if It helps anyone based on angular 4.3, a recent example of this I had to do myself was:

@Injectable()
export class LoadingBarInterceptor implements HttpInterceptor {

    private _pending = new Subject();
    private _pendingRequests: number = 0;

    public constructor(private loadingBarService: SlimLoadingBarService) {
        this._pending.subscribe((progress: any) => {
            if (progress.started) {
                this.loadingBarService.start();
            }
            if (progress.completed) {
                this.loadingBarService.complete();
            }
        });
    }

    public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        this._requestStarted();
        return next.handle(req).finally(
            () => this._requestEnded()
        );
    }

    private _requestStarted() {
        this._pending.next({
            started: this._pendingRequests === 0,
            pendingRequests: ++this._pendingRequests,
        });
    }

    private _requestEnded() {
        this._pending.next({
            completed: this._pendingRequests === 1,
            pendingRequests: --this._pendingRequests,
        });
    }

}

Then in your app.module.ts:

@NgModule({
    ...
    providers: [
        { provide: HTTP_INTERCEPTORS, useClass: LoadingBarInterceptor, multi: true }
    ]
    ...
})

Stu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants