全局配置
You can configure this in either the config
or the run
method. If your configurations don't need any other services, then I'd recommend you do them in the config
. If your configurations depend on other services, you can configure them in the run
using Restangular
instead of RestangularProvider
在 config
里配置
app.config(function(RestangularProvider) {
RestangularProvider.setBaseUrl('/api/v1');
RestangularProvider.setExtraFields(['name']);
RestangularProvider.setResponseExtractor(function(response, operation) {
return response.data;
});
RestangularProvider.addElementTransformer('accounts', false, function(element) {
element.accountName = 'Changed';
return element;
});
RestangularProvider.setDefaultHttpFields({cache: true});
RestangularProvider.setMethodOverriders(["put", "patch"]);
// In this case we are mapping the id of each element to the _id field.
// We also change the Restangular route.
// The default value for parentResource remains the same.
RestangularProvider.setRestangularFields({
id: "_id",
route: "restangularRoute",
selfLink: "self.href"
});
RestangularProvider.setRequestSuffix('.json');
// Use Request interceptor
RestangularProvider.setRequestInterceptor(function(element, operation, route, url) {
delete element.name;
return element;
});
// ..or use the full request interceptor, setRequestInterceptor's more powerful brother!
RestangularProvider.setFullRequestInterceptor(function(element, operation, route, url, headers, params, httpConfig) {
delete element.name;
return {
element: element,
params: _.extend(params, {single: true}),
headers: headers,
httpConfig: httpConfig
};
});
});
在 run
里配置
// Here I inject the service BaseUrlCalculator which I need
app.run(function(Restangular, BaseUrlCalculator) {
Restangular.setBaseUrl(BaseUrlCalculator.calculate());
});