Run hook before $ resource creates url?

I want to programmatically change the route parameters before $ resource creates a url. I cannot use the angular http interceptor to do this, since the route is already concatenated at this point.

Given 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 } }); }; 

... and some controller.js

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

How can I use a hook that will always convert {model: "user"} to {model: "user"}

+5
source share
1 answer

I would say that you should go for tranformRequest over the $resource get part.

code

 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; }, } }); }; 

Please answer the link here , but you should save part of the conversion request to $ resource get .

+2
source

Source: https://habr.com/ru/post/1239403/


All Articles