本文介绍了AngularJS承诺,$ Q,推迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改

第一个答案是优雅的,但是,在这个问题说了几次,计算器上的另一个问题,这个问题是服务和控制器上运行自己的事情的数据实际上到达之前。

The first answer is the elegant one, but, as stated a few times in this question and another questions on stackoverflow, the problem is that the service and the controller run their thing before the data actually arrives.

(第一个答案最新的留言:)

(Last comment on the first answer:)

是的,问题是,API调用完成服务奔跑之后
  并返回一切控制器,看这里
  screencast.com/t/uRKMZ1IgGpb7 ......这是我的基本问题,我怎么可能
  等待所有部件的数据到达?

这就像我说的它的重复,我们如何作出这样的填充成功的数据检索后阵列的服务,并在控制器后,这一切让数据发生,因为你可以看到我的截图,事情不同的顺序执行。

It's like I'm saying it on repeat, how do we make a service that populates the array after the successful data retrieval, and the controller getting data after all this happens, because as you can see in my screenshot, things run in a different order.

我有这样的code:

 var deferred = $q.defer();
            $http.get('../wordpress/api/core/get_category_posts/?category_id=14 ').success(function(data) {
                //we're emptying the array on every call
                theData = [];
                catName = data.category.slug;
                theData = data;
                theData.name = catName;
                aggregatedData.push(theData);
            });
            $http.get('../wordpress/api/core/get_category_posts/?category_id=15 ').success(function(data) {
                theData = [];
                catName = data.category.slug;
                theData = data;
                theData.name = catName;
                aggregatedData.push(theData);
            });
            $http.get('../wordpress/api/core/get_category_posts/?category_id=16 ').success(function(data) {
                theData = [];
                catName = data.category.slug;
                theData = data;
                theData.name = catName;
                aggregatedData.push(theData);
            });
            $http.get('../wordpress/api/core/get_category_posts/?category_id=17 ').success(function(data) {
                theData = [];
                catName = data.category.slug;
                theData = data;
                theData.name = catName;
                aggregatedData.push(theData);
            });
            //deferred.resolve(aggregatedData);
            $timeout(function() {
                deferred.resolve(aggregatedData);
            }, 1000);
            /*//deferred.reject('There is a connection problem.');
            if (myservice._initialized) {
                $rootScope.$broadcast('postsList', deferred.promise);
            }*/
            //myservice._initialized = true;
            myservice = deferred.promise;
            return deferred.promise;

有关我的生活我不明白为什么我把超时使所得到的阵列时推迟?

For the life of me I can't understand why do I have to put a timeout when passing the resulting array to defer ?

不应该的原则是这样,推迟信息来等待,然后返回承诺?那是什么1秒存在的意义呢?从我的理解推迟应该可以,只要需要等待API返回的结果和回报承诺的数据。

Shouldn't the principle be like, defer waits for the information to come and then returns the promise? What is the point of that 1 second there? From what I understand defer should be able to wait as long as needed for the API to return the result and the return the promised data.

我真的很困惑,我撞了我的头靠在墙上的最后两个小时,因为我没有在我的控制器接收到任何数据,只有当我把超时那里。

I'm really confused, I've banged my head against the walls for the last two hours because I was not receiving any data in my controller, only when I put that timeout there.

推荐答案

恕我直言,我觉得有一个非常聪明的(优雅)的方式与 $ q.all 来做到这一点。

IMHO I think there's a much clever (and elegant) way to do this with $q.all.

请看看下面的code。

Please take a look at the code below.

我假设你想在一次与一个对所有大的阵列上的结果返回数据。

I am assuming that you want to return the data at once with all the results aggregated on a big array.

var myApp = angular.module('myApp', []);

myApp.factory('myService', function ($http, $q) {
    return {
        getAllData: function () {
            return $q.all([
                $http.get('../wordpress/api/core/get_category_posts/?category_id=14'),
                $http.get('../wordpress/api/core/get_category_posts/?category_id=15'),
                $http.get('../wordpress/api/core/get_category_posts/?category_id=16'),
                $http.get('../wordpress/api/core/get_category_posts/?category_id=17')
            ]).then(function (results) {
                var aggregatedData = [];
                angular.forEach(results, function (result) {
                    aggregatedData = aggregatedData.concat(result.data);
                });
                return aggregatedData;
            });
        }
    };
});

您可以在上面看到的 aggregatedData 只有在所有异步调用都通过 $ q.all

You can see above that the aggregatedData is only generated once all the async calls are completed via the $q.all.

您只需要包括服务作为依赖到您的控制器之一,例如,并调用这样的服务 myService.getAllData()

You just need to include the service as dependency into one of your controllers, for example, and call the service like this myService.getAllData()

希望帮助或只是让我知道,如果你需要一个完整的工作的例子,我可以提供一个! :)

Hope that helps or just let me know if you need a full working example and I can provide one! :)

这篇关于AngularJS承诺,$ Q,推迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 07:55