gpt4 book ai didi

arrays - ng-repeat 没有显示 AngularJS 1.5 中的 JSON 数组列表,但我可以单独看到值

转载 作者:行者123 更新时间:2023-12-01 14:37:50 25 4
gpt4 key购买 nike

我使用 CakePHP 3 在后端使用 REST 架构,根据 firebug 返回具有此格式的 JSON 数组,这是一个 200 OK,响应如下:

    {
"recipes": [
{
"id": 0,
"name": "modificadodsadasdas"
},
{
"id": 1,
"name": "dasdasdasdasdas"
}
]
}

我的index.html文件:

<html>
<head>

</head>
<body ng-app="AppRecetas">
<div ng-controller="RecetasCtrl">
<ul ng-repeat="recipe in recipes">
<li>{{recipe.name}}</li>
</ul>
</div>
<script src="js/angular.min.js"></script>
<script src="js/consumidor_rest.js"></script>
</body>
</html>

我的 consumidor_rest.js 从 REST Web 服务(位于同一服务器上)获取数据:

var app = angular.module("AppRecetas", []);

app.controller("RecetasCtrl", function($scope, $http) {
$http.get('http://localhost/services/recipes/index.json').
success(function(data, status, headers, config) {
$scope.recipes = data;
}).
error(function(data, status, headers, config) {
// log error
});
});

Firebug 的控制台没有显示任何错误,并且 ajax 调用正确且响应正确。但 ng-repeat 只是显示 ul 的一个点,没有任何值(value)。如果我在 ng-repeat 内部使用:

 <li>{{recipe[0].name}}</li>

我可以看到第一个元素正确显示。出了什么问题?我猜这是cakePHP如何传递数组的问题,但我不确定……

最佳答案

检查您从 http 调用中获取的数据对象。我认为您的数据对象是 JSON 对象,对象内的“recipes”属性是您的数组。因此,请考虑将 $scope.recipes = data 更改为$scope.recipes = data.recipes

为了验证 UI 中的数据对象,请打印如下内容:

{{ $scope.recipes | json }}

关于arrays - ng-repeat 没有显示 AngularJS 1.5 中的 JSON 数组列表,但我可以单独看到值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36770992/

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