简体   繁体   English

Angular.JS绑定不起作用

[英]Angular.JS binding isn't working

I have an extremely simple setup here 我这里有一个非常简单的设置

index.html 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: 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. 当我单击“获取位置”按钮时,我的绑定{{city.Latitude}}和{{city.Longitude}}保持空白。

I tried debugging by setting a break point in Chrome, and my values do show up. 我尝试通过在Chrome中设置断点来进行调试,但我的值确实出现了。 So I'm not sure what I'm missing. 所以我不确定我缺少什么。 Any help? 有什么帮助吗?

I'm using AngularJS 1.6 我正在使用AngularJS 1.6

在此处输入图片说明

The parameter passed to the then function is the response object. 传递给then函数的参数是响应对象。 The response object contains the data: 响应对象包含以下数据:

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

Is the location returned the response from the web service? 该位置是否从Web服务返回了响应?

Did you actually want to do: 您是否真的想做:

$scope.city = location.data;

You want the data object returned in the get function. 您希望在get函数中返回data对象。

Try $scope.city = location.data 试试$scope.city = location.data

得到响应后,请使用$ scope.apply()开始新的摘要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM