Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was also looking for a way to handle nested resources with ngResource. I am not familiar with how rails works or what your data looks like but this is what I had:</p> <pre><code>{"num_results": 5, "objects": [....], "page": 1, "total_pages": 1} </code></pre> <p>I needed to reach in and grab the nested objects array for the query action. Using the 1.0 version of angular this is not possible. However, with the 1.1 version (I tested with 1.1.3) it is possible to do this.</p> <p>In my controller I just setup the resource like this:</p> <pre><code>$scope.MyModel = $resource("/api/mymodel/:id", {}, {'query': {method: 'GET', isArray: true, "transformResponse": function (data) { return JSON.parse(data).objects; }}}); </code></pre> <p>The key here is the <code>transformResponse</code> function passed as part of the action config. In 1.1 any extra config items in the action config are passed down into the <code>$http</code> config for the request. The <code>$http</code> service allows a <code>transformResponse</code> function that can manipulate the data returned by the request. Using this function I can reach into the nested structure and return the array I need for the action.</p> <p>One caveat to notice here is that the transformResponse function receives a string value, so you must first parse the data into what you are expecting. The other caveat is that you return the actual final data value you want, NOT a string. Even though you receive a string value you should return the end data value required.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload