angular - how to get resource parameter from custom transformResponse

StackOverflow https://stackoverflow.com/questions/20301779

  •  06-08-2022
  •  | 
  •  

سؤال

is it possible to get resource param from transformResponse function?

demoApp.factory('Social', function($resource) {
  return $resource('api/social/:platform', {
    platform: '@platform'
  }, {
    getAdmin: {
      method: 'GET',
      isArray: false,
      transformResponse: function(resp) {
        // so, can i get `platform` in line 3 from here?
        return resp[platform];
      }
    }
  });
});

as shown in the code above, can i get the parameter platform from the transformResponse function?

هل كانت مفيدة؟

المحلول

'@platform' as a parameter means that you will be 'GET'ting the url 'api/social/', and putting the value of the returned objects 'platform' parameter into storage.

When calling the remaining non-get functions of the resource, (by default 'save', 'remove', 'delete'), the $resource will modify the url by replacing ':platform' with the previously saved value.

So, since you are in the get request, you can obtain the parameter exactly as you've specified.

transformResponse: function(resp)
{
    // so, can i get `platform` in line 3 from here?
    console.log(resp.platform); // Yes you can
    return resp[platform];
}

HOWEVER, since the data being returned is now resp.platform, the url parameter is being set to resp.platform.platform... Probably not what you want, and I'm guessing the source of what's not working for you.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top