最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular promises using multiple http requests - Stack Overflow

programmeradmin4浏览0评论

I have read a few forum posts before on angular promises but can't get it to work in my instance. I am using nodejs /lootive for the backend and Angular form the frontend.

I have the following code in a controller, basically I want to use the path to slides.path, how would I go about doing this using promises? any help would be gratefully received.

function ProductCtrl($scope, $http, $q) {

    $scope.events = [];
    $scope.times = [];

    var html = [];
    var chapters = [];
    var path;

    //var paPromise = $q.defer();

    $http({
        url: '/show',
        method: 'GET',
        params: { eventid:$scope.$routeParams.eventid}

    }).success(function(response, code) {

        $scope.events = response;

        angular.forEach($scope.events.slides, function(slide) {

            $http({
                url: '/upload',
                method: 'GET',
                params: {uploadid: slide.upload.toString()}

            }).success(function(response, code) {
                return "/"+response.path;

            },path);

            slide.path = path;

            chapters.push(slide);

        });

    });
}

I have read a few forum posts before on angular promises but can't get it to work in my instance. I am using nodejs /lootive for the backend and Angular form the frontend.

I have the following code in a controller, basically I want to use the path to slides.path, how would I go about doing this using promises? any help would be gratefully received.

function ProductCtrl($scope, $http, $q) {

    $scope.events = [];
    $scope.times = [];

    var html = [];
    var chapters = [];
    var path;

    //var paPromise = $q.defer();

    $http({
        url: '/show',
        method: 'GET',
        params: { eventid:$scope.$routeParams.eventid}

    }).success(function(response, code) {

        $scope.events = response;

        angular.forEach($scope.events.slides, function(slide) {

            $http({
                url: '/upload',
                method: 'GET',
                params: {uploadid: slide.upload.toString()}

            }).success(function(response, code) {
                return "http://www.example./"+response.path;

            },path);

            slide.path = path;

            chapters.push(slide);

        });

    });
}
Share Improve this question edited Apr 16, 2014 at 1:27 Ivan Nevostruev 28.8k8 gold badges69 silver badges82 bronze badges asked Apr 16, 2014 at 1:26 Richard McRichard Mc 1622 silver badges14 bronze badges 2
  • 1 You are doing it with promises. $http returns a promise, and the success call is only invoked when the promise is resolved. What exactly are you having a problem with? – adrichman Commented Apr 16, 2014 at 2:47
  • Unless you want to go full FRP or enable legacy auto unwinding mode. I'm not sure what you want to acplish. – Benjamin Gruenbaum Commented Apr 16, 2014 at 4:59
Add a ment  | 

1 Answer 1

Reset to default 4

You can use $q.all do get this multiple promise problem done. Like this:

function ProductCtrl($scope, $http, $q) {

$scope.events = [];
$scope.times = [];

var html = [];

var path;

function fetchChapter() {
    var chapters = [];

    var httpPromise = $http({
        url: '/show',
        method: 'GET',
        params: {
            eventid: $scope.$routeParams.eventid
        }
    });

    //Return the value http Promise
    return httpPromise.then(function (response) {
        $scope.events = response;
        var promises = [];
        angular.forEach($scope.events.slides, function (slide) {

            var inPromise = $http({
                url: '/upload',
                method: 'GET',
                params: {
                    uploadid: slide.upload.toString()
                }
            }).then(function (response, code) {
                //each promise makes sure, that he pushes the data into the chapters
                slide.path = "http://www.example./" + response.path;
                chapters.push(slide);
            });
            //Push the promise into an array
            promises.push(inPromise);
        });
        //return the promise from the $q.all, that makes sure, that all pushed promises are ready and return the chapters.
        return $q.all(promises).then(function () {
            return chapters;
        });
    });
}

fetchChapter().then(function(chapters){
   //populate here 
});

}

The httpPromise will return the promise from $q.all.

EDIT: How to fetch the data then Wrap a function around, i did with fetchChapter and pass a function into the then there will be the value you need as a parameter.

发布评论

评论列表(0)

  1. 暂无评论