0

I am new to AngularJS and wanted to try out the $resource functionality.

I have this code:

 .factory('GetTasksService', function($resource, BASE_URL) {
        return $resource(BASE_URL + 'api/tasks');
    })
    .controller('TasksCtrl', function ($scope, $http, BASE_URL, GetTasksService) {

        $scope.tasks = GetTasksService.query();

        $scope.getTasks = function () {
            $http({ url: BASE_URL + 'api/tasks', method: 'GET' })
            .success(function (data, status, headers, config) {
                $scope.tasks = data; 
            })
            .error(function (data, status, headers, config) {
                alert("call did not work");
            });
        }
    });

The getTasks function works as expected - returning an array of:["taska", "taskb"] as it should.

The GetTasksService.query() however returns an array of [{"0":"t","1":"a","2":"s","3":"k","4":"a"}, {"0":"t","1":"a","2":"s","3":"k","4":"b"}]

Can anyone tell me what I am doing wrong?

Stefan
  • 1,590
  • 3
  • 18
  • 33
  • This has been [answered before](http://stackoverflow.com/questions/13813673/one-dimensional-array-of-strings-being-parsed-to-2d-by-angular-resource?rq=1), basically ngResource expects an object or an array of objects. You're returning an array of strings. – ikumen Aug 13 '14 at 19:47

1 Answers1

0

There is an option when specifying an action for an ngResource called isArray;

The default action query sets this as true by default, so you need to set set the action like:

query : {
     method : 'GET',
     isArray : false

  }
Streetfights
  • 103
  • 6