0

For testing with angular I've a Webapi method which returns an instance of an object that conatins a List. (The json output can be seen below)

Now in my angular controller i want to retrieve this object.

Does anyone have a hint what I'm doing wrong?

RestResource as factory

myngApp.factory("valueResource", function ($resource) {

    var result =
        {
            database: $resource("/api/values")
        }

    return result;
});

Controller:

myngApp.controller("dataController", function ($scope, settings, valueResource) {
    $scope.message = "Hello World!";
    $scope.userName = settings.user;


    $scope.restValues = valueResource.database.query({});
});

the json response (taken with fiddler) is this:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpca2lsblxleHBlcmltZW50YWxcc3JjXG5nMlxXZWJBcHBsaWNhdGlvbjFcV2ViQXBpXHZhbHVlcw==?=
X-Powered-By: ASP.NET
Date: Mon, 17 Feb 2014 14:47:35 GMT
Content-Length: 19

{"Items":[1,2,3,4]}

Console output is:

XHR finished loading: "http://localhost:55671/api/values". angular.js:8013
Error: [$resource:badcfg] http://errors.angularjs.org/1.2.12/$resource/badcfg?p0=array&p1=object
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:6:450
    at p.then.m.$resolved (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular-resource.min.js:8:517)
    at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:93:5)
    at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:93:5)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:94:173
    at h.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:102:456)
    at h.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:100:218)
    at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:103:264)
    at f (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:67:120) angular.js:9435

2 Answers 2

3

Looking at the error and its documentation

http://docs.angularjs.org/error/$resource/badcfg

it is clear that query should return an array, whereas yours is an object with a single property Items.

Use the get method on the resource.

Sign up to request clarification or add additional context in comments.

1 Comment

Thx i expected that for testing single objects would work. My fault :-)
1

The documentation for $resource indicates that creating models should be done in a different manner. If you just want to make an XHR request to this URL, I'd use the $http service instead and resolve the promise this returns in the view.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.