1

My question is similar to an existing question.

I created a service (CoffeeScript)

dpmp.factory 'Keyword', ($resource) ->
  $resource '/keywords:id', {}, {query: {method: 'GET', isArray: false}}

which gets the right http response,

{"title":"sysomos","subtitle":"Top keywords by mentions","unit":"times","data":[{"category":"partager","quantity":9753},{"category":"keyword1","quantity":6352},{"category":"keyword2","quantity":6311},{"category":"keyword3","quantity":2983},{"category":"keyword4","quantity":10}]}

I want to get data out of the resource in the controller, i.e.

$scope.keywords = Keyword.get()

But this code does not work. When I do

console.log($scope.keywords)

I get functions, instead of the data, back. The data is also already there, but I cannot get the data out:

> Resource {$get: function, $save: function, $query: function, $remove: function, $delete: function}
  > data: Array[5]
    subtitle: "Top keywords by mentions"
    title: "sysomos"
    unit: "times"
  > __proto__: Resource

For example, console.log($scope.keywords.data) returns undefined.

Any solution? Or can you give me an idea what is going on here?

1
  • 1
    The question you linked to answers your question. Commented Aug 13, 2013 at 10:05

1 Answer 1

1

Try the below

  Keyword.get(function(data){
 $scope.keywords=data
})
Sign up to request clarification or add additional context in comments.

2 Comments

this just works. Although it works, I am not clear why I do not have access to the data object though I already have the data with $scope.keywords = Keyword.query(). I can see the data on my Chrome console! In theory, we should be able to do $scope.keywords = Keyword.query(), then $scope.$watch "keywords" -> doSomething(), right?
since resource is asyncronous so immediately if you do console.log you will get undefined

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.