在返回之前更新q defer promise

时间:2016-04-08 17:19:56

标签: angularjs

我从工厂调用getBookIDs并使用结果从同一个工厂调用getBookInfo。但在Console.log(bookInfo)中,它显示了之前调用的结果! 如何在返回之前更新deferred.promise值?

这是我的控制器

angular.module('myApp.products',[])
    .controller('productController', function ($scope , MainFactory , $location) {
        function getBookInfo(bookIDs){
            MainFactory.getBookList(bookIDs)
                .then(function (bookInfo) {
                    console.log(bookInfo)
                })
        }

         MainFactory.getBookIDs()
             .then(function (result) {
                 $scope.bookIDList = result;
                 getBookInfo($scope.bookIDList);
             });


    });

这是我的工厂

app = angular.module('myApp');

app.factory("MainFactory", ['$soap', '$http', '$q', function ($soap, $http, $q) {
    var viewFactory = {};
    var deferred = $q.defer();
    viewFactory.getBookIDs = function () {
        //var bookIDs = [];
        $http({
            url: 'http://127.0.0.1/client.php?fn=getBooks',
            method: "GET"
        }).then(function success(response) {
            deferred.resolve(response.data.result);
        }, function myError(error) {
            console.log('error', error);
        });
        return deferred.promise;
    };

    viewFactory.getBookList = function (bookIDs) {
        $http({
            url: 'http://127.0.0.1/client.php?fn=getBooksInfo&p1=' + bookIDs,
            method: "GET"
        }).then(function success(response) {
            deferred.resolve(response.data.result);
        }, function myError(error) {
            deferred.reject(error);
        });
        return deferred.promise;
    };

    return viewFactory;
}]);

2 个答案:

答案 0 :(得分:0)

所有角度服务都是单身,所以我猜你得到这个错误的原因是getBookIDsgetBookList共享同一个deferred

尝试将工厂更改为

app.factory("MainFactory", ['$soap', '$http', '$q', function ($soap, $http, $q) {
    var viewFactory = {};
    viewFactory.getBookIDs = function () {
        //var bookIDs = [];
        var deferred = $q.defer();
        $http({
            url: 'http://127.0.0.1/client.php?fn=getBooks',
            method: "GET"
        }).then(function success(response) {
            deferred.resolve(response.data.result);
        }, function myError(error) {
            console.log('error', error);
        });
        return deferred.promise;
    };

    viewFactory.getBookList = function (bookIDs) {
        var deferred = $q.defer();
        $http({
            url: 'http://127.0.0.1/client.php?fn=getBooksInfo&p1=' + bookIDs,
            method: "GET"
        }).then(function success(response) {
            deferred.resolve(response.data.result);
        }, function myError(error) {
            deferred.reject(error);
        });
        return deferred.promise;
    };

    return viewFactory;
}]);

答案 1 :(得分:0)

您应该为每种服务方法返回新的承诺:

app.factory("MainFactory", ['$soap', '$http', '$q', function ($soap, $http, $q) {
    var viewFactory = {};
    viewFactory.getBookIDs = function () {
        var deferred = $q.defer();
        //var bookIDs = [];
        $http({
            url: 'http://127.0.0.1/client.php?fn=getBooks',
            method: "GET"
        }).then(function success(response) {
            deferred.resolve(response.data.result);
        }, function myError(error) {
            console.log('error', error);
        });
        return deferred.promise;
    };

    viewFactory.getBookList = function (bookIDs) {
        var deferred = $q.defer();
        $http({
            url: 'http://127.0.0.1/client.php?fn=getBooksInfo&p1=' + bookIDs,
            method: "GET"
        }).then(function success(response) {
            deferred.resolve(response.data.result);
        }, function myError(error) {
            deferred.reject(error);
        });
        return deferred.promise;
    };

    return viewFactory;
}]);

不应该重用Promise(除非你希望执行多个任务来触发相同的解析/拒绝......但是,我认为你应该为此明确地实现一个promise聚合器。)