我见过一些类似的回复,但它们没有解决我的问题。 html/JS 完全新手。
JSON 文件,托管在远程网络服务器上:
[
{"firstName": "John", "lastName": "Doe"},
{"firstName": "Anna", "lastName": "Smith"},
{"firstName": "Peter", "lastName": "Jones"}
]
这是我的 JS 代码。 $http.get
是一个网站,可让您将 JSON 数据存储到临时网络服务器中。
自动化.js:
var parsefile = angular.module("parser", []);
parsefile.controller("parserCtrl", function($scope, $http) {
$http.get("https://api.myjson.com/bins/wpkh").then(function(response) {
$scope.stuff = response.data;
});
});
我的 HTML 代码的一部分是:
<div ng-app="parser" ng-controller="parserCtrl">
<ul>
<li ng-repeat="x in stuff">
{{ stuff.firstName }}
</li>
</ul>
</div>
<script src="automate.js"></script>
但是当我运行它时,唯一打印的实际上是 {{stuff.firstName}}
。
有什么建议吗?
最佳答案
你有
<li ng-repeat="x in stuff">
{{ stuff.firstName }}
</li>
它将尝试打印stuff's
firstName
。改成
<li ng-repeat="x in stuff">
{{ x.firstName }}
</li>
并尝试检查您的服务器是否返回任何分配给 $scope.stuff
的数据
关于javascript - JSON 文件内容未在 HTML 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40859238/