Javascript 在angular中获取json数组中的第一项?

Javascript 在angular中获取json数组中的第一项?,javascript,json,angularjs,Javascript,Json,Angularjs,我正在尝试获取json数组中的第一项,该项是从我使用工厂调用的web服务中获取的 我的控制器: CreateProjectIndex.controller('GroupCtrl', ['$scope', 'GetGroups', function($scope, GetGroups){ $scope.groups = GetGroups.getGroups(); console.log($scope.groups); console.log($scope.groups[0]);

我正在尝试获取json数组中的第一项,该项是从我使用工厂调用的web服务中获取的

我的控制器:

CreateProjectIndex.controller('GroupCtrl', ['$scope', 'GetGroups',
function($scope, GetGroups){
   $scope.groups = GetGroups.getGroups();
   console.log($scope.groups);
   console.log($scope.groups[0]);
   $scope.group1 = $scope.groups[0];
}]);
我的服务:

'use strict';
var Groups = angular.module('GroupsService', ['ngResource']);

Groups.factory('GetGroups', ['$resource',
function($resource){
    return $resource('../../../api/Groups/GetGroups', {}, {
        getGroups : {method : 'GET', params:{}, headers: {'Accept': 'application/json;charset=UTF-8'}, isArray : true}
    });
}]);
“console.log($scope.groups);”返回:

[$promise: Object, $resolved: false]
0: Resource
    groupId: "361552"
    groupName: "1"
    __proto__: Resource
>1: Resource
>2: Resource
>3: Resource
>4: Resource
>5: Resource
$promise: Object
$resolved: true
length: 6
__proto__: Array[0]
而“console.log($scope.groups[0]);”只返回“undefined”


有没有办法获取该对象中的第一项

getGroup正在返回一个承诺。试试这个

CreateProjectIndex.controller('GroupCtrl', ['$scope', 'GetGroups',
function($scope, GetGroups){
   GetGroups.getGroups()
    .then(function(data){
      $scope.groups = data;
    });
}]);

然后,当该请求“已解决”时,$scope.groups将成为您从该请求返回的数据

getGroup正在返回一个承诺。试试这个

CreateProjectIndex.controller('GroupCtrl', ['$scope', 'GetGroups',
function($scope, GetGroups){
   GetGroups.getGroups()
    .then(function(data){
      $scope.groups = data;
    });
}]);

然后,当该请求“已解决”时,$scope.groups将成为您从该请求返回的数据

getGroup正在返回一个承诺。试试这个

CreateProjectIndex.controller('GroupCtrl', ['$scope', 'GetGroups',
function($scope, GetGroups){
   GetGroups.getGroups()
    .then(function(data){
      $scope.groups = data;
    });
}]);

然后,当该请求“已解决”时,$scope.groups将成为您从该请求返回的数据

getGroup正在返回一个承诺。试试这个

CreateProjectIndex.controller('GroupCtrl', ['$scope', 'GetGroups',
function($scope, GetGroups){
   GetGroups.getGroups()
    .then(function(data){
      $scope.groups = data;
    });
}]);

然后,当该请求“已解决”时,$scope.groups将成为您从该请求返回的数据

试试这个。它起作用了

 CreateProjectIndex.controller('GroupCtrl', ['$scope', 'GetGroups',
    function($scope, GetGroups){
       $scope.groups = GetGroups.getGroups();
       $scope.groups.$promise.then(function(data) {
           alert(data[0].toSource());
       });

    }]);

试试这个。它起作用了

 CreateProjectIndex.controller('GroupCtrl', ['$scope', 'GetGroups',
    function($scope, GetGroups){
       $scope.groups = GetGroups.getGroups();
       $scope.groups.$promise.then(function(data) {
           alert(data[0].toSource());
       });

    }]);

试试这个。它起作用了

 CreateProjectIndex.controller('GroupCtrl', ['$scope', 'GetGroups',
    function($scope, GetGroups){
       $scope.groups = GetGroups.getGroups();
       $scope.groups.$promise.then(function(data) {
           alert(data[0].toSource());
       });

    }]);

试试这个。它起作用了

 CreateProjectIndex.controller('GroupCtrl', ['$scope', 'GetGroups',
    function($scope, GetGroups){
       $scope.groups = GetGroups.getGroups();
       $scope.groups.$promise.then(function(data) {
           alert(data[0].toSource());
       });

    }]);


getGroup方法返回的是promise对象,而不是数据。尝试使用类似GetGroups.GetGroups()的东西。然后(response){$scope.groups=response};谢谢,我已经看到了一些关于这个的东西,但是它给了我“TypeError:undefined不是一个函数”。知道这是怎么回事吗?getGroup方法返回的是promise对象,而不是数据。尝试使用类似GetGroups.GetGroups()的东西。然后(response){$scope.groups=response};谢谢,我已经看到了一些关于这个的东西,但是它给了我“TypeError:undefined不是一个函数”。知道这是怎么回事吗?getGroup方法返回的是promise对象,而不是数据。尝试使用类似GetGroups.GetGroups()的东西。然后(response){$scope.groups=response};谢谢,我已经看到了一些关于这个的东西,但是它给了我“TypeError:undefined不是一个函数”。知道这是怎么回事吗?getGroup方法返回的是promise对象,而不是数据。尝试使用类似GetGroups.GetGroups()的东西。然后(response){$scope.groups=response};谢谢,我已经看到了一些关于这个的东西,但是它给了我“TypeError:undefined不是一个函数”。你知道这是怎么回事吗?谢谢你,我看到了一些关于这个的东西,但它告诉我“TypeError:undefined不是函数”。你知道这是怎么回事吗?谢谢你,我看到了一些关于这个的东西,但它告诉我“TypeError:undefined不是函数”。你知道这是怎么回事吗?谢谢你,我看到了一些关于这个的东西,但它告诉我“TypeError:undefined不是函数”。你知道这是怎么回事吗?谢谢你,我看到了一些关于这个的东西,但它告诉我“TypeError:undefined不是函数”。你知道这是怎么回事吗?谢谢你,我看到了一些关于这个的东西,但它告诉我“TypeError:undefined不是函数”。你知道这是怎么回事吗?你能简单地发个短信吗?在这种情况下,你得到了TypeError。谢谢你,我看到了一些关于这个的东西,但它给了我“TypeError:undefined不是一个函数”。你知道这是怎么回事吗?你能简单地发个短信吗?在这种情况下,你得到了TypeError。谢谢你,我看到了一些关于这个的东西,但它给了我“TypeError:undefined不是一个函数”。你知道这是怎么回事吗?你能简单地发个短信吗?在这种情况下,你得到了TypeError。谢谢你,我看到了一些关于这个的东西,但它给了我“TypeError:undefined不是一个函数”。你知道这是怎么回事吗?你能简单地发短信吗?在这种情况下,你就有打字错误了。