ajax - Angular JS 数据未从异步 http 请求填充

标签 ajax jquery asynchronous angularjs

我正在尝试获取一个加载 div 以显示我的 Angular 模块何时联系服务器。我使用 AngularJS 1.0.1 和 AngularJS Resources 1.0.1 来扩展 HTML 和 jquery 1.7.2 的功能,以获得一些额外的 dom 操作和 ajax 支持。

我已经发现,如果我在 async false 中运行 ajax 调用,加载 div 将不会显示,但 Firefox 除外,它会显示,但动画 gif 不是动画的。

如果我以 async true 运行 ajax 调用,则返回的数据不会加载到已加载模板部分的 Angular 模型变量中。

是否可以获取动画加载 div 来显示并获取 Angular 数据以加载到模型中?

最佳答案

希望我的理解是正确的。您想要显示 ajax 请求的加载进度。为此,您需要一个响应拦截器。以下是如何执行此操作的示例 http://jsfiddle.net/zdam/dBR2r/

// register the interceptor as a service, intercepts ALL angular ajax http calls
.factory('myHttpInterceptor', function ($q, $window, $rootScope) {
    return function (promise) {
        $rootScope.nowloading = true; // use ng-show="nowloading" on element containing spinner
        return promise.then(function (response) {
            // do something on success
            $rootScope.nowloading = false; // need to turn it off on success 
            return response;

        }, function (response) {
            // do something on error
            $rootScope.nowloading = false;  // need to turn it off on failure
            $rootScope.network_error = true;   // you might want to show an error icon.
            return $q.reject(response);
        });
    };

希望这有帮助

--丹

关于ajax - Angular JS 数据未从异步 http 请求填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11673559/

相关文章:

jquery - 使用 navbar-fixed 和 materialize 时我的图标消失了

javascript - 禁用具有按钮的特定 tr 的输入复选框

c++计时器稍后调用函数/方法?

javascript - 如何在 Angular 中制作异步函数?

javascript - Zend Controller 未从 Jquery/ajax 接收数据

javascript - 无法正确指定 CORS 请求的 header

javascript - 为什么在使用 ajax 调用填充表后不使用 jquery 函数?

jQuery 在简单计算中返回 NaN

javascript - 从循环nodejs中的函数获取值

javascript - 如何将事件绑定(bind)到 Squarespace AjaxLoader?