gpt4 book ai didi

Angularjs $resource 不适用于从 REST API 返回的数组

转载 作者:行者123 更新时间:2023-12-03 21:04:00 26 4
gpt4 key购买 nike

我正在尝试学习 angularjs,并且在尝试将数据绑定(bind)到从 Rest API 返回的数组时遇到了障碍。我有一个简单的 azure api 返回一组人员对象。服务网址为 http://testv1.cloudapp.net/test.svc/persons .

我的 Controller 代码如下所示:

angular.module("ToDoApp", ["ngResource"]);
function TodoCtrl($scope, $resource) {
$scope.svc = $resource('http://testv1.cloudapp.net/test.svc/persons', {
callback: 'JSON_CALLBACK'
}, {
get: {
method: 'JSONP',
isArray: true
}
});
$scope.items = $scope.svc.get();
$scope.msg = "Hello World";
}

我的 html 看起来像:
<html>
<head></head>
<body>
<div ng-app="ToDoApp">
<div ng-controller="TodoCtrl">
<h1>{{msg}}</h1>

<table class="table table-striped table-condensed table-hover">
<thead>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
</thead>
<tbody>
<tr ng-repeat="item in items">
<td>{{item.Email}}</td>
<td>{{item.FirstName}}</td>
<td>{{item.LastName}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>

问题:上面 html 中的表格没有显示任何数据。我可以在 Firebug 中看到对 api 的调用,也可以看到来自 api 的 JSON 响应。我做错了什么导致与 REST api 的数据绑定(bind)不起作用?

PS:JSFiddle 演示此问题位于: http://jsfiddle.net/jbliss1234/FBLFK/4/

最佳答案

只是想交叉发布我对 Aleksander 在另一个 stackoverflow 问题上给出的解决方案的改编:https://stackoverflow.com/a/16387288/626810 :

methodName: {method:'GET', url: "/some/location/returning/array", transformResponse: function (data) {return {list: angular.fromJson(data)} }}

所以当我调用这个函数时:
var tempData = ResourceName.methodName(function(){
console.log(tempData.list); // list will be the array in the expected format
});

如果您需要在多个方法/资源中获取数组,显然这有点笨重,但它似乎工作。

关于Angularjs $resource 不适用于从 REST API 返回的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15725445/

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