【发布时间】:2016-04-28 17:23:15
【问题描述】:
如何从 ng 资源中获取 url(和参数)?
如果这不可能,我该如何创建包装器或扩展 ng-resource 以便获得此功能?
更新:我希望能够从资源对象中提取 URL 和参数,而无需发送请求。
在理想世界中,以下工作:
myResource = $resource('localhost/foo/bar');
myResource.getUrl() // returns localhost/foo/bar
由于 getUrl 函数不存在,我尝试了在https://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;
}
]);
我注入新工厂并试用:
res = ExtendedResourceFactory('url')
res.getArguments()
Uncaught TypeError: res.getArguments is not a function(…)
感谢指导或解决方案!
【问题讨论】:
-
我也需要它来进行单元测试。我想做:
$httpBackend.expectGET(MyResource.getUrl()).respond(200, fixture.get)
标签: javascript angularjs