0

私は次のコントローラーを持っています:

.controller( 'AppCtrl', function AppCtrl ( $scope, $location, $resource ) {
    var Card = $resource('http://localhost/card/:cardId',
      {
        'cardId': "@_id"
      },
      {
      getAll: {
        method: 'GET',
        transformResponse: function (data) {
          return angular.fromJson(data)._items;
        },
        isArray: true
      }
    });

    $scope.allCards = Card.getAll();
    console.log($scope.allCards);
    console.log($scope.allCards[0]);

これで、コンソールに次の 2 行が表示されます。

[$resolved: false, $then: function]
    > 0: c
    > 1: c
    > 2: c
    > 3: c
    > 4: c
    > 5: c
    > 6: c
    > 7: c
    > 8: c
    > 9: c
    > 10: c
    > 11: c
    > 12: c
    > 13: c
      $resolved: true
    > $then: function (callback, errback) {
      length: 14
    > __proto__: Array[0]

$scope.allCards の値としてですが、$scope.allCards[0] の値としては「未定義」です。$scope.allCards を ng-repeat に渡すことはできますが、インデックスで項目を選択したいので、これを行うことができる配列を取得すると仮定しました。

「カード」のメソッドが返すオブジェクトの種類と、そこから配列を取得するにはどうすればよいですか?

4

1 に答える 1

1

isArray: trueメソッドは promise を返しますが、それらに直接作用することはできません。

関数に渡すことができます。

var cards = Card.getAll(function () { cards[0]; });

于 2013-06-09T23:42:08.867 に答える