1

Problem Question : I am making call to API and it returns the object back. Now I want to add the empty array to this object and use it in my view. How would i achieve this with angular?

// Controller This works as the data was manually

$scope.deliveryData = [
            {
                productName: 'ProductA ProductA ProductA', quantity: '20', deliveries: []
            },
            {
                productName: 'Product B', quantity: '10', deliveries: []
            }
        ];

// controller This does not work :( how would I add (deliveries:[]) to this ?

 $scope.deliveryData; // This will be now populated from API 
     MyService.delivery()
        .success(function(data){
            $scope.deliveryData= data;
            console.log(data) //this data does not contain the empty array, how i append to it?
        })
        .error(function(status,data){
            console.log(status);
            console.log(data);
        })

I don't know how to achieve this in angular, even I don't know if I am doing this right way. Please give me any suggestions.

UPDATE 1

 .success(function(data){
                $scope.deliveryData = data;
                console.log("Before Change");
                console.log($scope.deliveryData);
                for (var i = 0; i < $scope.deliveryData.length; i++) {
                    $scope.deliveryData[i].deliveries = [];
                }
                console.log("After Change");
                console.log($scope.deliveryData);
            })

Data coming from API in following format:

-Object {delivery: Array[1]}
 --delivery: Array[1]
   ---0:Object
   ----name: "ProductA ProductA ProductA"
   ----quantity: 14 
4
  • Is cart the same as deliveryData above? Commented Dec 1, 2014 at 2:16
  • @NewDev sorry i was making changes when server went down and did not pick it up. Please see my updated question :) Commented Dec 1, 2014 at 2:18
  • Can you give an example of what the deliveryData should look like? Commented Dec 1, 2014 at 2:19
  • @NewDev your solution is close, but it is not adding to the $scope.deliveryData instead it is adding to array of object Commented Dec 1, 2014 at 2:35

1 Answer 1

1

To add {deliveries: []} to each product item, here is what you should do:

    .success(function(data){
        $scope.deliveryData = data.delivery;
        for (var i = 0; i < $scope.deliveryData.length; i++) {
            $scope.deliveryData[i].deliveries = [];
        }
    })

UPDATE

It's basic stuff, there's no way it doesn't work, see it here:

var $scope = {};

$scope.deliveryData = [ // no deliveries here
    {productName: 'ProductA ProductA ProductA', quantity: '20'},
    {productName: 'Product B', quantity: '10'}
];

for (var i = 0; i < $scope.deliveryData.length; i++) {
    $scope.deliveryData[i].deliveries = [];
}

document.getElementsByTagName('pre')[0].innerHTML = JSON.stringify($scope, null, '\t');
<pre></pre>

11
  • somehow it does not add the array to that object :(. I am printing the object to the console can't see anything Commented Dec 1, 2014 at 2:32
  • You're printing data, my answer modifies $scope.deliveryData which is what you use after you fetch the data. Commented Dec 1, 2014 at 2:40
  • no mate i am printing $scope.deliveryData, its not in there Commented Dec 1, 2014 at 2:41
  • That's impossible. Can you please show me your current code? (you can add it as an update to the question) Commented Dec 1, 2014 at 2:49
  • Please see my question, I might be doing something wrong Commented Dec 1, 2014 at 3:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.