0

I am trying to output the result from the success function as following, but am having no luck. The code does return values for UpcomingEvents and if I output that to the html, it works, but when I am passing it onto the returnlist, it does not work.

    $scope.Events = {}
    $scope.returnlist = {};
    var PriorID = 67;
    var i = 0;
    var j = 0;

    //I am calling http get url function which is working fine.
       .success(function (response) {
        console.log(response);
        $scope.Events = response;
        //I am able to display Events[0].ID in html.

        if ($scope.Events[0].ID == PriorID)
        //The condition holds true, so it will go inside the loop. I have  
         confirmed that with a debugger.
        {
            newID = $scope.Events[0].ID;
            newname = $scope.Events[0].Title;
            $scope.returnlist[0] = [{ ID: newID, Name: newname }];
            $scope.returnlist[1] = [{ ID: newID, Name: newname }];
            //through debugger breakpoints, I have found that returnlist is  
            getting the correct values. 

        }

    })

Everything works well until I try to display the values in my html. I am trying it like this.

                {{returnlist[0].ID}}

I have even tried it like this:

             <tr ng-repeat="data in returnlist">
              <td>returnlist.ID</td>

but no luck. What am I doing wrong?

Thanks.

1 Answer 1

1

first you define $scope.returnlist = {}; which is an object not an array.

Then, when you add item you assign an array not an item of array:

$scope.returnlist[0] = [{ ID: newID, Name: newname }];

So your final returnlist will look like:

{0: [{ ID: newID, Name: newname }], 1:[{ ID: newID, Name: newname }],...}

an object with array value, so when you do $scope.returnlist[0].ID you will get undefined.

The right way is:

$scope.returnlist = [];//an array

//in the loop now:
$scope.returnlist[0] = { ID: newID, Name: newname };//an item not an array
Sign up to request clarification or add additional context in comments.

Comments

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.