1

I need to trigger similar to JQuery events ajaxStart and ajaxStop I found a half solution here -> How to set default HTTP header in Angular2? So, at the moment i can handle an ajaxStart Event.

Any solutions how to handle ajaxStop event?

post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
    //onAjaxStart
    $( "#loader" ).show();
    return super.post(url, body, options); //Any method here to handle event?
}

I tried to use .catch(), but it fires only onError

I need to call my animate loader when ajax fires, and hide it when it stops every time i use ajax request.

So i am trying to override default ajax methods. But i can't find right observable method(

Community
  • 1
  • 1
ArtemSky
  • 1,173
  • 11
  • 20
  • This is unrelated to your question, but instead of using jQuery why don't you have an if statement on the elements and show/hide them based on a variable inside of your Component? I think that's generally the Angular way of doing things. – Dehli Apr 25 '16 at 15:15

1 Answers1

1

You could leverage the "finally" operator of observable.

Here is a sample of use:

post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
    //onAjaxStart
    $( "#loader" ).show();
    return super.post(url, body, options).finally(() => {
      //onAjaxStop
      $( "#loader" ).hide();
    });
}
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • Yeah its cool, but i need to return an oservable object. Because i want to call this overrided method as simple post/get/etc request Yeah its cool, but i need to return an oservable object. Because i want to call this overrided method as simple post/get/etc request this.http.post("someult", string).map((res: Response) => res.json()) .subscribe().....etc – ArtemSky Apr 25 '16 at 15:52
  • This doesn't prevent from regarding the observable ;-) I mean the one returned by the finally method. When subscribing on it you will receive the result of the request... – Thierry Templier Apr 25 '16 at 15:54