2

I'm using AngularJS and I've made a $resource call to fetch a single value (e.g. the fetch gets me 14).

The following is in my controller:

var MaxCount = $resource('/maxquestion/fetch', null, 
{
    getMaxQuestionCount: {method: 'GET', isArray: false}
});
$scope.maxQuestionCount = MaxCount.getMaxQuestionCount();

Now I'm trying to use $scope.maxQuestionCount in the controller, doing $scope.maxQuestionCount[0] gives me 1 and $scope.maxQuestionCount[1] gives me 4

console.log($scope.maxQuestionCount) me [object Object] console.dir($scope.maxQuestionCount) me There are no child objects

I'm confused. How to access the value 14?

1 Answer 1

5

There is really not much point in using $resource for data structures like those. The thing is that $resource works great for RESTful endpoint where all the HTTP verbs are used. To make this easy AngualarJS extends incoming objects with convenience methods ($save, $delete etc.). If you return primitives there is no room for extension and one of the major benefits of $resource is gone.

In short: if you are just after fetching data $resource is an overkill IMO, stick with the $http instead. The $http service, although considered to be lower-level as compared to $resource is very convenient and easy to use. Taking your example, you could write:

$http.get('/maxquestion/fetch').success(function(result){
  console.log(result);
});

Refer to the documentation for more examples: http://docs.angularjs.org/api/ng.$http

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

2 Comments

Thanks for the comment. I'll surely take that into consideration and rewrite the code I've written. Thanks for the heads up.
Thanks a lot! Really appreciate it. I've changed it to $http and it works now. Awesome

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.