gpt4 book ai didi

javascript - $http 服务不在浏览器中显示来自 angularjs 中的 json 文件的数据

转载 作者:行者123 更新时间:2023-11-28 04:56:35 24 4
gpt4 key购买 nike

我编写了controller.js和HTML文件,但加载时console.log()显示JSON文件中的对象,但数据无法在浏览器中显示。

HTML:-

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>AngularJs|Http Service</title>
<script src="angular.min.js"></script>

<script src="controller16.js"></script>

</head>

<body ng-app="mainApp">
<div ng-controller="people">
<ul>
<li ng-repeat="person in users">{{person.Name}}</li>
</ul>
</div>

</body>

</html>

Controller .js:-

var app = angular.module('mainApp', []);
app.controller('people', function($scope, $http) {
$http.get("http://127.0.0.1/Angularjs/12/database.json").then(function(response) {
$scope.users = response.records;
console.log(response);
});
});
database.json: -{
"records": [{
"Name": "Alfreds Futterkiste",
"City": "Berlin",
"Country": "Germany"
}, {
"Name": "Ana Trujillo Emparedados y helados",
"City": "México D.F.",
"Country": "Mexico"
}, {
"Name": "Antonio Moreno Taquería",
"City": "México D.F.",
"Country": "Mexico"
}, {
"Name": "Around the Horn",
"City": "London",
"Country": "UK"
}, {
"Name": "B's Beverages",
"City": "London",
"Country": "UK"
}, {
"Name": "Berglunds snabbköp",
"City": "Luleå",
"Country": "Sweden"
}, {
"Name": "Blauer See Delikatessen",
"City": "Mannheim",
"Country": "Germany"
}, {
"Name": "Blondel père et fils",
"City": "Strasbourg",
"Country": "France"
}, {
"Name": "Bólido Comidas preparadas",
"City": "Madrid",
"Country": "Spain"
}, {
"Name": "Bon app'",
"City": "Marseille",
"Country": "France"
}, {
"Name": "Bottom-Dollar Marketse",
"City": "Tsawassen",
"Country": "Canada"
}, {
"Name": "Cactus Comidas para llevar",
"City": "Buenos Aires",
"Country": "Argentina"
}, {
"Name": "Centro comercial Moctezuma",
"City": "México D.F.",
"Country": "Mexico"
}, {
"Name": "Chop-suey Chinese",
"City": "Bern",
"Country": "Switzerland"
}, {
"Name": "Comércio Mineiro",
"City": "São Paulo",
"Country": "Brazil"
}]
}

上面的代码只显示了一个空白屏幕。show 帮我弄清楚。

最佳答案

使用resonse.data.record而不是response.record

$http.get("http://127.0.0.1/Angularjs/12/database.json").then(function(response) {
$scope.users = response.data.records;
console.log(response.data);
});

关于javascript - $http 服务不在浏览器中显示来自 angularjs 中的 json 文件的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42501650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com