AngularJS $资源将整个对象编码为URL

时间:2016-01-11 14:54:33

标签: javascript angularjs typescript angular-resource angular-http

我将Angular 1.4.8与Angular UI和TypeScript结合使用。我的模型定义如下:

export interface IBatch extends ng.resource.IResource<IBatch> {
    id: Number;
    ...
}

export interface IBatchResource extends ng.resource.IResourceClass<IBatch> {
    snapshot(batch: IBatch);
}

我使用自定义HTTP动词Batch设置我的TAKE-SNAPSHOT资源,该动词返回200 OK404 NOT FOUND

var paramDefaults = {
    id: '@id'
};

var actions = {
    'snapshot': { method: 'TAKE-SNAPSHOT' }
};

return <IBatchResource> this.$resource('/api/batches/:id', paramDefaults, actions);

这让我可以拍摄特定批次的快照。此API调用的 only 参数是批处理ID。但是,$resource正在将整个 Batch对象编码到查询字符串中(实际字符串长度超过1000个字符,为简洁起见缩短了):

  

本地主机:15000 / API /批次/ 4 $ originalData =%7B%22id%22:4,%22createdDateUtc%22:%222015-12 -...

如何$resource将请求定向到localhost:15000/api/batches/4

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题:

var paramDefaults = {
    id: '@id'
};

var actions = {
    'snapshot': <ActionDescriptor>{ method: 'TAKE-SNAPSHOT' }
};

var retval = <IBatchResource> this.$resource('/api/batches/:id', paramDefaults, actions);

/*
 * Passing in the object results in serializing the entire Batch into the URL query string.
 * Instead, we only want to pass the ID.
 */
var oldSnapshotFn = retval.snapshot;
retval.snapshot = (batch: IBatch) => oldSnapshotFn(<any>{id: batch.id });
return retval;