Reputation: 1468
EDIT: I found my solution. Angular doesn't seem to push to a regular javascript array, but it performs as expected if it's a $scope.array. Any comments on whether or not this isn't the Angular way are appreciated.
I am using Angular to build a service retrieving scores based on zipcodes, and I'm running into an issue of scope I don't understand.
My API returns a value for each zipCode that's fed to the function, which I'm then attempting to append to an Array.
However, the code below throws an error:
cannot call method push() of undefined
Which tells me that my scoreArray is not making its way into the scope of the promise resolution.
How can I make this function fruitful and return the array after the for loop completes its run?
var $scope.scoreArray = [];
var zipArray = [10031,55325,83832];
function getScores(zipArray,scoreArray){
for(i=0;i<zipArray.length;i++){
Scoreboard.getScores(zipArray[i])
.then(function(score){
$scope.scoreArray.push(score);
})
}
return scoreArray;
}
Upvotes: 0
Views: 61
Reputation: 44632
array.push() returns the length of the new array. You're overwriting it every time with an int value.
Upvotes: 3