我在Angular UI和TypeScript中使用Angular 1.4.8。我的模型定义如下:

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调用的唯一参数是批处理ID。但是,$resource将整个Batch对象编码为查询字符串(实际字符串的长度超过1000个字符,为简洁起见已缩短):


  本地主机:15000 / api /批处理/ 4?$ originalData =%7B%22id%22:4,%22createdDateUtc%22:%222015-12 -...


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

最佳答案

我设法解决:

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;

关于javascript - AngularJS $ resource正在将整个对象编码为URL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34724433/

10-16 21:00