2016-11-29 61 views
0

我见过类似的回复,但没有解决我的问题。在html/JS中完成新手。未载入HTML的JSON文件内容

JSON文件,托管在远程Web服务器:

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

这里是我的JS代码。 $http.get是一个网站,可让您将JSON数据存储到临时网络服务器中。

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

任何提示?

回答

3

你有

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

将尝试打印stuff'sfirstName。它变成

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

而且还尝试检查,如果你的服务器返回的任何数据,其中分配给我曾经花了3个小时的$scope.stuff

+0

最愚蠢的错误。我想所有程序员都会遇到这种情况......感谢新鲜的眼睛! – ellusion