无法使用 AngularJS $resource 检索 JSON 数组

Unable to retrieve JSON array with AngularJS $resource

我一直在尝试从 JSON 中检索值,但到目前为止没有成功。当我刷新页面时,它确实在前端被调用,但信息没有传递给下一个方法。我认为问题可能出在 promises.push... 行,因为我试图调试下面的方法,但根本没有传递信息。

AngularJS:

var promises = [];
promises.push(SpringDataRestService.get({"collection": "subjects"}).$promise);

// Require each of these queries to complete before continuing
$q.all(promises).then(function (data) {
    // Grab the first result
    $scope.available = data[0].subjects;
    $scope.selected = [];
    // If this is an update, get the second result in set
    if (data.length > 1) {
        // For each permission that is assigned to this role, add ID (name) to selected
        for (var i = 0; i < data[1].data.subjects.length; i++) {
            var perm = data[1].data.subjects[i];
            $scope.selected.push(perm.name);
        }
    }
    $scope.tableEditOptions = new NgTableParams({}, {
        dataset: $scope.available
    });
   $scope.available, 'name');
}).catch(function (data) {
  // ERROR
});

JSON:

[
  {
    "name": "FWGWG",
    "description": "WGWGWG",
    "lockId": 0
  },
  {
    "name": "QFQFQF",
    "description": "QFQFQFQ",
    "lockId": 0
  }
]

我也确信我的 for 循环是错误的,因为也分配了值,因为我认为它不应该是 data.subjects,但我知道这些线程每个问题只有 1 个问题.任何帮助将不胜感激。

对数组使用query方法:

var promise = SpringDataRestService.query({"collection": "subjects"}).$promise;

promise.then(function (dataArr) {
    console.log(dataArr);
    //...
}).catch(function (errorResponse) {
    console.log(errorResponse);        
});

对于 REST 服务,get 方法 returns 一个 JavaScript 对象,query 方法 returns 一个 JavaScript 数组。

来自文档:

$resource Returns

A resource "class" object with methods for the default set of resource actions optionally extended with custom actions. The default set contains these actions:

{
  'get':    {method: 'GET'},
  'save':   {method: 'POST'},
  'query':  {method: 'GET', isArray: true},
  'remove': {method: 'DELETE'},
  'delete': {method: 'DELETE'}
}

...

It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data.

有关详细信息,请参阅