我怎样才能从NG-资源的网址? [英] How can I get the url from an ng-resource?

查看:148
本文介绍了我怎样才能从NG-资源的网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能从NG-资源的URL(而params)?

How can I get the url (and params) from an ng-resource?

如果我怎样才能创建一个包装或延长NG-资源,所以我得到这个功能?这是不可能的。

And if that is not possible how can I create a wrapper or extend ng-resource so I get this functionality?

更新:我希望能够提取资源对象的URL,而params而没有发送请求。

Update: I want to be able to extract the URL and params from the resource object without ever sending a request.

在一个理想的世界了以下工作:

In an ideal world the following works:

myResource = $resource('localhost/foo/bar');

myResource.getUrl() // returns localhost/foo/bar

由于使用getURL功能不存在我想我在<一个发现了以下href=\"http://stackoverflow.com/a/28474289/4829972\">http://stackoverflow.com/a/28474289/4829972

一个包装厂:

angular.module('app').factory('ExtendedResourceFactory',['$resource',
  function($resource) {
    function ExtendedResourceFactory() {
      var Resource = $resource.apply(this, arguments);

      Resource.prototype.getArguments = function() {
        return arguments
      };

      return Resource;
    }

    return ExtendedResourceFactory;
  }
]); 

我注入了新的工厂,并尝试一下:

I inject the new factory and try it out:

res = ExtendedResourceFactory('url')
res.getArguments()

Uncaught TypeError: res.getArguments is not a function(…) 

感恩路或解决方案!!

Thankful for directions or solutions!!

推荐答案

也许下面将使用。既然你的URL的显式控制被传递到$资源,我想你可以改变的响应,提供你想要的功能。它绝不是干净的,但确实出现功能

Maybe the following will be of use. Since you have explicit control of the URL being passed into $resource, I assume you can transform the response to provide the function you desire. It is by no means clean, but does appear functional.

var resource;

resource = $resource('/foo/bar', 
  {
    id: 1
  }, {
    get: {
      method: 'GET',
      transformResponse: function(data) {
        data = angular.fromJson(data);
        data.getUrl = function() {
          return '/foo/bar';
        };
        return data;
      }
    }
  }
);

这篇关于我怎样才能从NG-资源的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆