JSON文件内容未在HTML中加载

时间:2016-11-29 06:35:28

标签: javascript html angularjs json

我已经看到了几个类似的回复,但他们没有解决我的问题。在html / JS中完成新手。

JSON文件,托管在远程网络服务器上:

[
  {"firstName": "John", "lastName": "Doe"},
  {"firstName": "Anna", "lastName": "Smith"},
  {"firstName": "Peter", "lastName": "Jones"}
]

这是我的JS代码。 $http.get是一个允许您将JSON数据存储到临时Web服务器的网站。

automate.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}}

任何提示?

1 个答案:

答案 0 :(得分:3)

你有

<li ng-repeat="x in stuff">
    {{ stuff.firstName }}
 </li>

会尝试打印stuff's firstName。将其改为

<li ng-repeat="x in stuff">
    {{ x.firstName }}
 </li>

并尝试检查您的服务器是否返回了您分配给$scope.stuff

的任何数据