0

I have an extremely simple setup here

index.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
</head>
<body ng-app="locationApp" ng-controller="locationController">
    <button ng-click="getLocation()">Get Location</button>
    <br />
    Latitude: {{city.Latitude}} <br />
    Longitude {{city.Longitude}} <br />
    <script src="Scripts/lib/angular.min.js"></script>
    <script src="Scripts/app.js"></script>
</body>
</html>

app.js:

var locationApp = angular.module('locationApp', []);

var locationController = locationApp.controller("locationController", function ($scope, $http) {
    $scope.getLocation = function () {
        $http.get("https://localhost:44320/api/location?cityName=sg").then(function (location) {
            $scope.city = location;
        });
    }
});

When I click on the Get Location button, my bindings {{city.Latitude}} and {{city.Longitude}} remains blank.

I tried debugging by setting a break point in Chrome, and my values do show up. So I'm not sure what I'm missing. Any help?

I'm using AngularJS 1.6

enter image description here

2
  • Try Latitude: {{locationController.city.Latitude}} <br /> Commented Jan 26, 2017 at 15:11
  • 2
    it's location.data in your case. The get request is returning a whole request object, and you want '.data'. Commented Jan 26, 2017 at 15:13

4 Answers 4

2

The parameter passed to the then function is the response object. The response object contains the data:

$http.get("https://localhost:44320/api/location?cityName=sg").then(function (response) {
    $scope.city = response.data;
});
Sign up to request clarification or add additional context in comments.

Comments

1

Is the location returned the response from the web service?

Did you actually want to do:

$scope.city = location.data;

Comments

1

You want the data object returned in the get function.

Try $scope.city = location.data

Comments

-2

Use $scope.apply() after getting the response to start the new digest.

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.