【发布时间】:2014-09-28 05:02:59
【问题描述】:
我正在使用 AngularJS 和一些 jQuery 插件制作一个页面,其中 angular-js 控制器通过异步调用 ajax 的一些 servlet 方法来初始化其模型。我想在第一个 ajax 调用开始时显示加载 gif,并在最后一个 ajax 调用完成时隐藏它。
由于我不知道最后一个 ajax 调用完成,我不知道在哪里放置隐藏加载 gif 的指令。我怎样才能实现这种行为?
我想要的代码示例:
myApp.controller("adminCtrl", function($scope, $http) {
$scope.initData1 = function() {
/* is this the first call? then show the gif */
$http(...).success(function() { /* is this the last response received? then hide the gif */});
}
$scope.initData2 = function() {
/* is this the first call? then show the gif */
$http(...).success(function() { /* is this the last response received? then hide the gif */});
}
$scope.initData3 = function() {
/* is this the first call? then show the gif */
$http(...).success(function() { /* is this the last response received? then hide the gif */});
}
initData1();
initData2();
initData3();
}
我希望你能理解我的问题并知道任何方法来实现这一点。
【问题讨论】:
-
我接受@Valentin Waeselynck 的回答,因为它可以在多个控制器中重复使用(这是我的情况),而且我不确定 Chandermani 使用 Promise 的回答是否适用于 IE8+ 等浏览器(这也是我的案子)。尽管如此,我认为 Chandermani 关于 promises 的回答是现代浏览器的最佳方法。加载栏也很酷。
标签: javascript jquery ajax angularjs