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

Bra*_*n B 4 ajax jquery asynchronous angularjs

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

我已经发现,如果我在async false中运行ajax调用,则不显示加载div,除了Firefox显示但动画gif没有动画.

如果我在async中运行ajax调用,则返回的数据不会加载到已加载模板partial的角度模型变量中.

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

Dan*_*yon 12

希望我能正确理解你.您想要显示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);
        });
    };
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助

- 担