2017-01-26 115 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