$ resource中的条件transformRequest。$ save

问题描述 投票:0回答:1

我广泛使用$resource。我希望能够在某些路线/资源上设置$ save的transformRequest属性,但不能在其他路由/资源上设置。我怎样才能做到这一点?

$resource(url, [paramDefaults], [actions], options);定义中,您只能在自定义动作上设置transformRequest,而不是$save方法。

$httpProvider.defaults.transformRequest中,您无法确定您所在的路线/方法,因为您无法访问url / path,只能访问params数据。

angularjs angular-resource
1个回答
0
投票

我能想到的最好的:

$httpProvider.interceptors.push(function () {
    return {
        "request": function (config) {
            if (config.method === "POST" && new RegExp("{myregexhere}").test(config.url)) {
                // keep only the default
                config.transformRequest.length = 1;
            }
            return config;
        }
    };
});
© www.soinside.com 2019 - 2024. All rights reserved.