2017-01-26 114 views
0

我有一个非常简单的设置在这里Angular.JS约束力不工作

的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; 
     }); 
    } 
}); 

当我点击获取地点按钮,我的绑定{{city.Latitude}}和{{city.Longitude}}保持空白。

我试着在Chrome中设置一个中断点来调试,并且我的值显示出来。所以我不确定我错过了什么。任何帮助?

我越来越开始新摘要响应后使用AngularJS 1.6

enter image description here

+0

尝试'纬度:{{locationController.city.Latitude}}
' –

+2

它'location.data'你的情况。获取请求正在返回整个请求对象,并且您需要'.data'。 – theleebriggs

回答

2

传递给then函数的参数是响应对象。响应对象包含以下数据:

$http.get("https://localhost:44320/api/location?cityName=sg").then(function (response) { 
    $scope.city = response.data; 
}); 
1

该位置是否返回了来自Web服务的响应?

你真的想做的事:

$scope.city = location.data; 
1

要在get函数返回的data对象。

尝试$scope.city = location.data