AngularJS: 访问 $resource 对象中的数组
AngularJS: access Array within $resource object
我的 oracle REST-API return 是一个 JSON,带有来自数据库 table 的元组数组。
我正在使用 angularJs 的 $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
对象中提取数组时,return 只是一个空洞/未实现的承诺。我尝试了什么:
$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>
但是如何在我的 html 中的 $resource-object 中循环遍历数组(例如使用 ng-repeat
(?))?
Request 是异步操作,所以你需要通过 $promise 属性.
在其回调中处理来自 request 的数据
示例来自 AngularJs docs:
var User = $resource('/user/:userId', {userId: '@id'});
User.get({userId: 123}).$promise.then(function(user) {
// here you can assign your fetched data to $scope
});
您只需将代码更改为以下内容:
$resource(url, {}, {
query: {
method: 'GET',
transformResponse: function (data) { return data.items; },
isArray: true
}
}).query(function (result) {
$scope.dbData = result;
});
我的 oracle REST-API return 是一个 JSON,带有来自数据库 table 的元组数组。
我正在使用 angularJs 的 $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
对象中提取数组时,return 只是一个空洞/未实现的承诺。我尝试了什么:
$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>
但是如何在我的 html 中的 $resource-object 中循环遍历数组(例如使用 ng-repeat
(?))?
Request 是异步操作,所以你需要通过 $promise 属性.
在其回调中处理来自 request 的数据示例来自 AngularJs docs:
var User = $resource('/user/:userId', {userId: '@id'});
User.get({userId: 123}).$promise.then(function(user) {
// here you can assign your fetched data to $scope
});
您只需将代码更改为以下内容:
$resource(url, {}, {
query: {
method: 'GET',
transformResponse: function (data) { return data.items; },
isArray: true
}
}).query(function (result) {
$scope.dbData = result;
});