Arrays AngularJS:访问$resource对象内的数组
我的OracleREST-API返回一个JSON,其中包含来自数据库表的元组数组。 我正在使用angularJs的Arrays AngularJS:访问$resource对象内的数组,arrays,angularjs,rest,resources,Arrays,Angularjs,Rest,Resources,我的OracleREST-API返回一个JSON,其中包含来自数据库表的元组数组。 我正在使用angularJs的$resourceAPI访问数据,因此我的结果如下所示: Resource {$promise: Promise, $resolved: false} $promise:Promise {$$state: {…}} $resolved: true count: 25 hasMore: true items: Array(25) 0:{metricavgcount: 0, i
$resource
API访问数据,因此我的结果如下所示:
Resource {$promise: Promise, $resolved: false}
$promise:Promise {$$state: {…}}
$resolved: true
count: 25
hasMore: true
items: Array(25)
0:{metricavgcount: 0, instid: 1, metric: "SQL Service Response Time", unit: "CentiSeconds Per Call", threshold: 3, …}
1:{metricavgcount: 1, instid: 1, metric: "Physical Reads Per Sec", unit: "Reads Per Second", threshold: 10000, …}
2:{metricavgcount: 2, instid: 1, metric: "Average Active Sessions", unit: "Active Sessions", threshold: 50, …}
[...]
当我试图从$resource
对象中提取数组时,返回的只是一个空的/未实现的承诺。我尝试的是:
$scope.dbData = $resource(url, {}, {
query: {
method: 'GET',
transformResponse: function (data) { return data.items;},
isArray: true
}});
所以我想我只需要将$resource传递给视图,然后在那里执行其余的操作
在我能找到的每个教程中,人们只是在对象数组中循环,看起来很简单:
<div ng-repeat="dbDat in dbData"> {{dbData.myVariable}} </div>
{{dbData.myVariable}
但是我如何在html中的$resource对象中循环一个数组(例如,使用
ng repeat
(?))呢?请求是异步操作,所以您需要通过$promise属性在其回调中处理来自请求的数据
来自AngularJs的示例:
您只需将代码更改为以下内容:
$resource(url, {}, {
query: {
method: 'GET',
transformResponse: function (data) { return data.items; },
isArray: true
}
}).query(function (result) {
$scope.dbData = result;
});
非常感谢你!我真的需要更多地了解如何处理承诺。。。!
$resource(url, {}, {
query: {
method: 'GET',
transformResponse: function (data) { return data.items; },
isArray: true
}
}).query(function (result) {
$scope.dbData = result;
});