Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

ngx-restangular

> 前端框架
Open source

Restangular for Angular 2 and higher versions

776 stars0 likes1 views
WebsiteGitHub

About

Restangular for Angular 2 and higher versions

Ngx-restangular. Maintained by 2muchcoffee.

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.

Demo

You can check post about using ngx-restangular in RESTful API Angular Solution

Current stage

Ngx-restangular almost all functionality was transferred from the Restangular. We are open to any cooperation in terms of its further development.

Renaming project from ng2-restangular to ngx-restangular

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.

Table of contents

  • How do I add this to my project in angular 5+?

  • How do I add this to my project in angular 4?

  • How do I add this to my project in angular 2?

  • Dependencies

  • Starter Guide

    • Quick configuration for Lazy Readers
    • Using Restangular
      • Creating Main Restangular object
      • Lets Code with Observables!
      • Here is Example of code with using promises!
    • Configuring Restangular
      • Properties
        • withConfig
        • setBaseUrl
        • setExtraFields
        • setParentless
        • addElementTransformer
        • setTransformOnlyServerElements
        • setOnElemRestangularized
        • addResponseInterceptor
        • addFullRequestInterceptor
        • addErrorInterceptor
        • setRestangularFields
        • setMethodOverriders
        • setDefaultRequestParams
        • setFullResponse
        • setDefaultHeaders
        • setRequestSuffix
        • setUseCannonicalId
        • setPlainByDefault
        • setEncodeIds
      • Accessing configuration
      • How to configure them globally
        • Configuring in the AppModule
        • Configuring in the AppModule with Dependency Injection applied
      • How to create a Restangular service with a different configuration from the global one
      • Decoupled Restangular Service
    • Methods description
      • Restangular methods
      • Element methods
      • Collection methods
      • Custom methods
    • Copying elements
    • Using values directly in templates with Observables
    • URL Building
    • Creating new Restangular Methods
    • Adding Custom Methods to Collections
      • Example:
    • Adding Custom Methods to Models
      • Example:
  • FAQ

    • How can I handle errors?
    • I need to send Authorization token in EVERY Restangular request, how can I do this?
    • I need to send one header in EVERY Restangular request, how can I do this?
    • How can I send a delete WITHOUT a body?
    • I use Mongo and the ID of the elements is _id not id as the default. Therefore requests are sent to undefined routes
    • What if each of my models has a different ID name like CustomerID for Customer
    • How can I send files in my request using Restangular?
    • How do I handle CRUD operations in a List returned by Restangular?
    • Removing an element from a collection, keeping the collection restangularized
    • How can I access the unrestangularized element as well as the restangularized one?
    • How can add withCredentials params to requests?
  • Server Frameworks

  • Contributing

  • License

Back to top

How do I add this to my project in angular 5+?

You can download this by npm and running npm install ngx-restangular. This will install latest version of ngx-restangular (v.2.0.0).

Back to top

How do I add this to my project in angular 4?

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.

Back to top

How do I add this to my project in angular 2?

You can download this by npm and running npm install ng2-restangular

Back to top

Dependencies

Restangular depends on Angular 2+ and Lodash.

Back to top

Starter Guide

Quick Configuration (For Lazy Readers)

This is all you need to start using all the basic Restangular features.

…

` Back to top

Using Restangular

Creating Main Restangular object

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.

javascript
// 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);

Back to top

Lets Code with Observables!

Now that we have our main Object let's start playing with it.

…

`

Back to top

Here is Example of code with using promises!

…

`

Back to top

Configuring Restangular

Properties

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

withConfig

You can configure Restangular "withConfig" like in example below, you can also configure them globally RestangularModule or in service with withConfig

…

`

setBaseUrl

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.

setExtraFields

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

setParentless

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:

  • Boolean: Specifies if all elements should be parentless or not
  • Array: Specifies the routes (types) of all elements that should be parentless. For example ['buildings']

addElementTransformer

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)

setTransformOnlyServerElements

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).

setOnElemRestangularized

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:

  • elem: The element that has just been restangularized. Can be a collection or a single element.
  • isCollection: Boolean indicating if this is a collection or a single element.
  • what: The model that is being modified. This is the "path" of this resource. For example buildings
  • Restangular: The instanced service to use any of its methods

This can be used together with addRestangularMethod (Explained later) to add custom methods to an element

javascript
service.setOnElemRestangularized((element, isCollection, what, Restangular) => {
  element.newField = "newField";
  return element;
});

addResponseInterceptor

The responseInterceptor is called after we get each response from the server. It's a function that receives this arguments:

  • data: The data received got from the s

Issues· 37 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptangularangular-2angular2api

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架