1

我需要触发类似于 JQuery 事件ajaxStartajaxStop 我在这里找到了一个半解决方案 -> 如何在 Angular2 中设置默认 HTTP 标头? 所以,目前我可以处理一个ajaxStart事件。

任何解决方案如何处理ajaxStop事件?

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

我尝试使用.catch(),但它只触发 onError

我需要在 ajax 触发时调用我的动画加载程序,并在每次我使用 ajax 请求时停止时隐藏它。

所以我试图覆盖默认的ajax方法。但我找不到正确的可观察方法(

4

1 回答 1

1

您可以利用 observable 的“finally”运算符。

这是一个使用示例:

post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
    //onAjaxStart
    $( "#loader" ).show();
    return super.post(url, body, options).finally(() => {
      //onAjaxStop
      $( "#loader" ).hide();
    });
}
于 2016-04-25T15:33:47.570 回答