0

Here is my code, problem is I can't transfer $scope.postcodes value to outside of $http.post().success() function. $scope.problem has no value. Please help! Thanks.

angular.module('propertyList', ['route'])  
.controller('PropertyController', ['$scope','$http', function ($scope, $http) {
    $scope.properties = [{"latitude":53.796318,"longitude":-1.536373},   {"latitude":51.507923946494,"longitude":-0.14952014363775},   {"latitude":53.81893054791,"longitude":-1.5845336370246},...]  
    $scope.postcodes = [];  
for(var i=0; i<$scope.properties.length; i++){  
    var lat = $scope.properties[i].latitude;  
    var lng = $scope.properties[i].longitude;  
    var geolocation = {  
    "geolocations" : [{  
        "longitude": lng,  
        "latitude": lat,  
        "radius": 100,  
        "limit": 1  
    },]  
    };  
$http.post('https://api.postcodes.io/postcodes', geolocation)  
    .success(function(data){  
    var location = angular.fromJson(data);  
    var postcode = location.result[0].result[0].postcode;  
    $scope.postcodes += postcode+',';  
    });  
}  
$scope.problem = $scope.postcodes;  //this line doesn't work.  
}]);  
Hippo
  • 1
  • 2
  • You can assign a value to a global var from inside a function quite easily. If the var already exists, simply assign it. If not, you can create it by leaving off the "var" part and just assigning it as if it were available, which it is. – Robusto May 22 '16 at 15:00
  • Thanks. But I tried both ways, it didn't work. If you look at my code, $scope.postcodes doesn't have "var" in front. – Hippo May 22 '16 at 15:11
  • Oh, my bad. I didn't see that it was Angular. Thought it was real JavaScript. ^_^ – Robusto May 22 '16 at 15:13

0 Answers0