Restangular for Angular 2 and higher versions
Restangular for Angular 2 and higher versions
This project is the follow-up of the Restangular. Ngx-restangular is an Angular 2+ service that simplifies common GET, POST, DELETE, and UPDATE requests with a minimum of client code. It's a perfect fit for any WebApp that consumes data from a RESTful API.
You can check post about using ngx-restangular in RESTful API Angular Solution
Ngx-restangular almost all functionality was transferred from the Restangular. We are open to any cooperation in terms of its further development.
This project was renamed from ng2-restangular to ngx-restangular due to implementation of Semantic Versioning by Angular Core Team. NPM name has also changed, you can install actual version of the project with npm install ngx-restangular.
You can download this by npm and running npm install ngx-restangular. This will install latest version of ngx-restangular (v.2.0.0).
You can download this by npm and running npm install --save [email protected]
Versions from 1.0.14 to 1.1.4 are deprecated. Npm warns you after their installation. Those versions would be removed.
You can download this by npm and running npm install ng2-restangular
Restangular depends on Angular 2+ and Lodash.
This is all you need to start using all the basic Restangular features.
…There are 3 ways of creating a main Restangular object. The first one and most common one is by stating the main route of all requests. The second one is by stating the main route and object of all requests.
// Only stating main route
Restangular.all('accounts')
// Stating main object
Restangular.one('accounts', 1234)
// Gets a list of all of those accounts
Restangular.several('accounts', 1234, 123, 12345);Now that we have our main Object let's start playing with it.
…`
…`
Restangular comes with defaults for all of its properties but you can configure them. So, if you don't need to configure something, there's no need to add the configuration. You can set all these configurations in RestangularModule to change the global configuration, you can also use the withConfig method in Restangular service to create a new Restangular service with some scoped configuration or use withConfig in component to make specified Restangular
You can configure Restangular "withConfig" like in example below, you can also configure them globally RestangularModule or in service with withConfig
…`
The base URL for all calls to your API. For example if your URL for fetching accounts is http://example.com/api/v1/accounts, then your baseUrl is /api/v1. The default baseUrl is an empty string which resolves to the same url that Angular2 is running, but you can also set an absolute url like http://api.example.com/api/v1 if you need to set another domain.
These are the fields that you want to save from your parent resources if you need to display them. By default this is an Empty Array which will suit most cases
Use this property to control whether Restangularized elements to have a parent or not. So, for example if you get an account and then get a nested list of buildings, you may want the buildings URL to be simple /buildings/123 instead of /accounts/123/buildings/123. This property lets you do that.
This method accepts 1 parameter, it could be:
['buildings']This is a hook. After each element has been "restangularized" (Added the new methods from Restangular), the corresponding transformer will be called if it fits.
This should be used to add your own methods / functions to entities of certain types.
You can add as many element transformers as you want. The signature of this method can be one of the following:
addElementTransformer(route, transformer): Transformer is called with all elements that have been restangularized, no matter if they're collections or not.
addElementTransformer(route, isCollection, transformer): Transformer is called with all elements that have been restangularized and match the specification regarding if it's a collection or not (true | false)
This sets whether transformers will be run for local objects and not by objects returned by the server. This is by default true but can be changed to false if needed (Most people won't need this).
This is a hook. After each element has been "restangularized" (Added the new methods from Restangular), this will be called. It means that if you receive a list of objects in one call, this method will be called first for the collection and then for each element of the collection.
I favor the usage of addElementTransformer instead of onElemRestangularized whenever possible as the implementation is much cleaner.
This callback is a function that has 4 parameters:
buildingsThis can be used together with addRestangularMethod (Explained later) to add custom methods to an element
service.setOnElemRestangularized((element, isCollection, what, Restangular) => {
element.newField = "newField";
return element;
});The responseInterceptor is called after we get each response from the server. It's a function that receives this arguments:
No open issues yet, or sync has not completed.