簡體   English   中英

Angular.JS綁定不起作用

[英]Angular.JS binding isn't working

我這里有一個非常簡單的設置

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

在此處輸入圖片說明

傳遞給then函數的參數是響應對象。 響應對象包含以下數據:

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

該位置是否從Web服務返回了響應?

您是否真的想做:

$scope.city = location.data;

您希望在get函數中返回data對象。

試試$scope.city = location.data

得到響應后,請使用$ scope.apply()開始新的摘要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM