如何在发送angularj http请求时显示加载消息

时间:2016-01-15 09:00:11

标签: javascript angularjs loading

我想在发送http请求时显示加载消息并在请求完成时隐藏加载,我想将此用于所有http请求。是否有角度的全局函数来做这个..

2 个答案:

答案 0 :(得分:0)

JavaScript HTTP请求不会阻止,您将提供在请求完成时调用的回调。这意味着您可以这样做(伪代码,因为您没有提供示例代码):

function callback(httpResponse) {
  hideLoadingMessage();
  dostuffWithResponse(httpResponse);
}
// callback is callled when HTTP request is complete
doHttpRequest(params, callback);
// the code after continues to execute despite the HTTP request not being ready
showLoadingMessage();

因此,在发送HTTP请求后代码继续执行。 HTTP请求完成后将调用回调。在实践中,您还需要处理错误。

答案 1 :(得分:0)

快速Google搜索为Angular返回了相当多的结果:

加载微调器可以实现onClick。

http://ngmodules.org/modules/angular-loading-spinner

以前问过类似情况的问题:

Showing Spinner GIF during $http request in angular

最后,codepen.io示例(现在可能会被破坏)

http://codetunnel.io/how-to-do-loading-spinners-the-angular-way/

我强烈建议在将来要求之前尝试。我们需要您已尝试过的代码示例或您现在所处的代码示例。除非您使用插件(您自己可以找到),否则这不是一段简单的代码。

我建议将来尝试提供一些背景和示例。