title: Links (relations) to REST resources in AngularJS
body: <p>I have a REST API, which returns User object, where its roles are specified via link to another object. So at <code>localhost/project/api/users/27/</code> is JSON object:</p>
  
  <pre><code>{
      "id": 42,
      "name": "John",
      "prijmeni": "Doe",
      "login": "johndoe",
      "nickname": null,
      "grade": 1.3,
      "roles": {
          "href": "http://localhost/project/api/users/1716/roles/"
      }
  }
  </code></pre>
  
  <p>What I'm trying to do is to get roles in controller. My User service looks like this:</p>
  
  <pre><code>projectServices.factory('User', ['$resource', 'UserRoles',
      function($resource, UserRoles, Role) {
          var User = $resource('http://localhost/project/api/users/:id', {}, {
              'query': {
                  method: 'GET',
                  isArray: true
              }
          });
          return User;
      }
  ]);
  </code></pre>
  
  <p>and I tried to add (to that resource code block):</p>
  
  <pre><code>User.prototype.roles= function(){
     return UserRoles.get({id:42});
  };
  </code></pre>
  
  <p>this one freezes browser when called in ngRepeat. So I tried</p>
  
  <pre><code>User.prototype.roles = UserRoles.get({id:42});
  </code></pre>
  
  <p>this works. Then I tried</p>
  
  <pre><code>User.prototype.roles = $resource(this.roles.href, {}, {
     'get': {
        method: 'GET',
        isArray: true
     }
  });
  </code></pre>
  
  <p>says, that <code>roles</code> is undefined. I also tried to add <code>transformResponse</code> param to User service <code>GET</code> action, but that function was never called.</p>
  
  <p>The second option works just perfectly fine - except, that I have to hardcode the user ID. Suitable solution would be with somehow getting the user ID for me (i tried <code>this.id</code>, but that didn't work). </p>
  
  <p>Perfect solution would be creating resource from given <code>href</code>, but as I can't access <code>roles</code> in prototype, I don't know how.</p>
  
  <p>Thanks for any advice. </p>