Dave Gordon Dave Gordon - 1 month ago 11
Javascript Question

AngularJs extracting values from JSON Object and attaching to $scope

I am receiving the following JSON data object

{
"ip": "**Removed**",
"country_code": "GB",
"country_name": "United Kingdom",
"region_code": "ENG",
"region_name": "England",
"city": "Plymouth",
"zip_code": "PL6",
"time_zone": "Europe/London",
"latitude": 50.442,
"longitude": -4.0828,
"metro_code": 0
}


How do I extract the first two values and attach them to the $scope so that they can be shown in the template when received.?

Answer

Assuming that you're getting it via http request your call would be like this:

  $http.get("./getData.php").success(function(data) {
      $scope.data = {};                 
      $scope.data["ip"] = data["ip"];
      $scope.data["country_code"] = data["country_code"];
  }