2

I am trying to access the data in an object that is returned using $resource. I thought it would be similar to accessing data inside an object by using a dot but this doesn't work. In the console I can see the object contains an array named "jobs" but I can't figure out how to access this. This is the code

Service:

jenkinsDashboard.factory('Dashboard', ['$resource', function ($resource) {
    return $resource(
        'http://ci.angularjs.org/view/AngularJS/api/json?pretty=true',
        {}, 
        { query: { method:'GET', params:{}, isArray:false }});
    }]);

Controller:

dashboardControllers.controller('DashboardCtrl', ['$scope', 'Dashboard',
function($scope, Dashboard) {
    $scope.allData = Dashboard.query();
    console.log($scope.allData.jobs);
    console.log($scope.allData);
}]);

Output:

undefined                                                                  
Resource {$promise: Object, $resolved: false, $get: function, $save: function, $query: function…}
Chantal
  • 959
  • 2
  • 12
  • 24
  • can you please copy from console yours $scope.allData.jobs it will be easier to help you – sylwester Jul 11 '14 at 12:27
  • I added the output to the post tnx – Chantal Jul 11 '14 at 12:39
  • 2
    $scope.allData.$promise.then(function (result) { console.log(result) }); can you please add this line and post output – sylwester Jul 11 '14 at 12:40
  • Resource {description: null, jobs: Array[4], name: "AngularJS", property: Array[0], url: "http://ci.angularjs.org/view/AngularJS/"…} Is what the output is. Still no idea though how I can use the array – Chantal Jul 11 '14 at 12:49

1 Answers1

1

I had the same issue using $resources, find below a restructuring of your controller

    dashboardControllers.controller('DashboardCtrl', ['$scope', 'Dashboard',
 function($scope, Dashboard) {

Dashboard.query().$promise.then(function (res) {
        $scope.allData = res;
        console.log($scope.allData.jobs);
        console.log($scope.allData);
    });
 }]);
Simba
  • 11
  • 2