我在Google PageSpeed API的JSON响应中使用ng-repeat时遇到了一个奇怪的问题。基本上,我有它的工作,但奇怪的是返回一个奇怪的BLANK UL,然后是正确的回复。
测试页:http://staging.converge.io/test-json
我的控制器:(一旦得到答案就会更改API密钥)
function FetchCtrl($scope, $http, $templateCache) {
$scope.method = 'GET';
$scope.url = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=http://www.web.com&key=AIzaSyA5_ykqZChHFiUEc6ztklj9z8i6V6g3rdc';
$scope.key = 'AIzaSyA5_ykqZChHFiUEc6ztklj9z8i6V6g3rdc';
$scope.strategy = 'mobile';
$scope.fetch = function() {
$scope.code = null;
$scope.response = null;
$http({method: $scope.method, url: $scope.url + '&strategy=' + $scope.strategy, cache: $templateCache}).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
}).
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
};
$scope.updateModel = function(method, url) {
$scope.method = method;
$scope.url = url;
};
}
HTML :
<ul ng-repeat="formattedResult in data.formattedResults">
<li ng-repeat="ruleResult in formattedResult">
<h4>{{ruleResult.localizedRuleName}}</h4>
<strong>Impact score</strong>: {{ruleResult.ruleImpact*10 | number:0}}
</li>
</ul>
感谢您的帮助!
答案 0 :(得分:1)
正如我在响应中看到的那样,formattedResults
不是一个数组,它是一个包含ruleResults
数组的对象。您需要更改访问数据的方式。
替换:
<ul ng-repeat="formattedResult in data.formattedResults">
<li ng-repeat="ruleResult in formattedResult">
<h4>{{ruleResult.localizedRuleName}}</h4>
<strong>Impact score</strong>: {{ruleResult.ruleImpact*10 | number:0}}
</li>
</ul>
使用
<ul>
<li ng-repeat="ruleResult in data.formattedResults.ruleResults">
<h4>{{ruleResult.localizedRuleName}}</h4>
<strong>Impact score</strong>: {{ruleResult.ruleImpact*10 | number:0}}
</li>
</ul>