Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I need to create an array based in the following features:

Every time the ng-repeat directive list an object the init() function automatically sends these parameter to the controller in order to get an object.

<div data-ng-repeat="user in users" data-ng-init="init(user.id)">

In the other side, the init() function receive these parameter and then returns an object.

// Suppose I have 5 parameters in order to get 5 publications
$scope.init = function(id){
    $http.get("getPublicationByID/"+id)
    .success(function(data, status, headers, config){
         // Here is the object that I need to create an array
         $scope.publication = data; }) 
    .error(function(data, status, headers, config){
        $log.error("Error!")})
}

My question is, how can I create an array based on all these objects?

share|improve this question

Instead of using ng-init on each ng-repeat element, you could easily loop through users object and call ajax for each user and collect all publications together.

For that you need to use $q API's .when & .all method to wait till all promises get resolved.

Code

getAllPublications function(){
    var promiseArray = [];
    $scope.publications = []
    angular.forEach($scope.users, function(user){
       promiseArray.push($q.when($http.get("getPublicationByID/"+user.id)));
    })
    $q.all(promiseArray).then(function(response){
       $scope.publications = response; //response is array of all publications
    })
}
share|improve this answer
    
Hi, I have explained the same question in more detail. Can you give a hand? This is the question [link] (stackoverflow.com/questions/38154332/…) – wilson Jul 1 at 21:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.