运行 $resource 构造 url 之前的钩子?

Run a hook before $resource constructs the url?

我想在 $resource 构造 url 之前以编程方式更改路由参数 。我不能使用 angular 的 http 拦截器来执行此操作,因为路由在那时已经连接。

给定一个 Assortment.model.js

module.exports = function($resource) {
    return $resource("", {}, {
        get: {
            url: "/assortment/:model/:id",
            method: "GET",
            params: {id: "@id", model: "@model"} //< this needs to be uppercase
        }
    });
};

...还有一些 controller.js

["Supplier", function(Supplier) {
    Supplier.Assortment.get({ id: 5, model: "user" })
}]

我如何强制执行始终将 {model: "user"} 转换为 {model: "User"}

的挂钩

我会说你应该参加 tranformRequest 而不是 $resource 比赛。

代码

module.exports = function($resource) {
  return $resource("", {}, {
    get: {
      url: "/assortment/:model/:id",
      method: "GET",
      params: {
        id: "@id",
        model: "@model"
      },
      transformRequest: function(data, headers) {
        //here you could have the transformation of parameters
        console.log(data);
        return data;
      },
    }
  });
};

参考 answer here 但您应该将转换请求部分保留在 $resource 的 get.