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

restangular

> 编程语言
Open source

AngularJS service to handle Rest API Restful Resources properly and easily

7.8K stars0 likes0 views
WebsiteGitHub

About

AngularJS service to handle Rest API Restful Resources properly and easily

Restangular

Restangular is an AngularJS 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.

Note This version of Restangular only supports Angular 1. For an Angular 2+ version of Restangular, check out ngx-restangular. It's a separate project with different maintainers, so issues regarding ngx-restangular should be reported over there :wink:

Learn Restangular! Try the live demo on plunkr. It uses the same example as the official Angular Javascript Project, but with Restangular! Or watch a video introduction of a talk I gave at Devoxx France about Restangular.

Table of contents

  • Restangular
  • Differences with $resource
  • How do I add this to my project?
  • Dependencies
  • Production apps using Restangular
  • Starter Guide
    • Quick configuration for Lazy Readers
    • Adding dependency to Restangular module in your app
    • Using Restangular
      • Creating Main Restangular object
      • Let's code!
    • Configuring Restangular
      • Properties
        • setBaseUrl
        • setExtraFields
        • setParentless
        • setDefaultHttpFields
        • addElementTransformer
        • setOnElemRestangularized
        • setResponseInterceptor
        • setResponseExtractor (alias of setResponseInterceptor)
        • addResponseInterceptor
        • setRequestInterceptor
        • addRequestInterceptor
        • setFullRequestInterceptor
        • addFullRequestInterceptor
        • setErrorInterceptor
        • setRestangularFields
        • setMethodOverriders
        • setDefaultRequestParams
        • setFullResponse
        • setDefaultHeaders
        • setRequestSuffix
        • setUseCannonicalId
        • setPlainByDefault
      • How to configure them globally
        • Configuring in the config
        • Configuring in the run
      • 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
    • Enhanced promises - Using values directly in templates
    • Using Self reference resources
    • 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 one header in EVERY Restangular request, how do I do this?
    • Can I cache requests?
    • Can it be used in $routeProvider.resolve?
    • My response is actually wrapped with some metadata. How do I get the data in that case?
    • 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 do I handle CRUD operations in a List returned by Restangular?
    • When I set baseUrl with a port, it's stripped out.
    • How can I access the unrestangularized element as well as the restangularized one?
    • Restangular fails with status code 0
    • Why does this depend on Lodash / Underscore?
    • How do I cancel a request?
  • Supported Angular versions
  • Server Frameworks
  • Releases Notes
  • License

Back to top

Differences with $resource

Restangular has several features that distinguish it from $resource:

  • It uses promises. Instead of doing the "magic" filling of objects like $resource, it uses promises.
  • You can use this in $routeProvider.resolve. As Restangular returns promises, you can return any of the methods in the $routeProvider.resolve and you'll get the real object injected into your controller if you want.
  • It doesn't have all those $resource bugs. Restangular doesn't have problem with trailing slashes, additional : in the URL, escaping information, expecting only arrays for getting lists, etc.
  • It supports all HTTP methods.
  • It supports ETag out of the box. You don't have to do anything. ETags and If-None-Match will be used in all of your requests
  • It supports self linking elements. If you receive from the server some item that has a link to itself, you can use that to query the server instead of writing the URL manually.
  • You don't have to create one $resource object per request. Each time you want to do a request, you can just do it using the object that was returned by Restangular. You don't need to create a new object for this.
  • You don't have to write or remember ANY URL. With $resource, you need to write the URL Template. In here, you don't write any urls. You just write the name of the resource you want to fetch and that's it.
  • It supports nested RESTful resources. If you have Nested RESTful resources, Restangular can handle them for you. You don't have to know the URL, the path, or anything to do all of the HTTP operations you want.
  • Restangular lets you create your own methods. You can create your own methods to run the operation that you want. The sky is the limit.
  • Support for wrapped responses. If your response for a list of element actually returns an object with some property inside which has the list, it's very hard to use $resource. Restangular knows that and it makes it easy on you. Check out https://github.com/mgonto/restangular#my-response-is-actually-wrapped-with-some-metadata-how-do-i-get-the-data-in-that-case
  • You can build your own URLs with Restangular objects easily. Restangular lets you create a Restangular object for any url you want with a really nice builder.

Let's see a quick and short example of these features

…

`

Back to top

How do I add this to my project?

You can download this by:

  • Using bower and running bower install restangular
  • Using npm and running npm install restangular
  • Downloading it manually by clicking here to download development unminified version or here to download minified production version
  • Using CdnJS CDN files:
xml

<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/restangular/1.5.1/restangular.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/restangular/1.5.1/restangular.min.js"></script>

Back to top

Dependencies

Restangular depends on Angular and Lodash (or Underscore).

Back to top

Production apps using Restangular

Each time, there're more Production WebApps using Restangular. If your webapp uses it and it's not in the list, please create an issue or submit a PR:

  • Life360 is using Restangular to build the WebApp version of their platform
  • Thomson Reuters is using Restangular for the new Webapp they've built
  • Quran.com is using Restangular for their alpha/beta app and soon to be main site
  • Worldcampus.co is using Restangular for their beta international students social network.
  • ENTSO-E Transparency Platform

Back to top

Starter Guide

Quick Configuration (For Lazy Readers)

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

javascript
// Add Restangular as a dependency to your app
angular.module('your-app', ['restangular']);

// Inject Restangular into your controller
angular.module('your-app').controller('MainCtrl', function($scope, Restangular) {
  // ...
});

The Restangular service may be injected into any Controller or Directive :) Note: When adding Restangular as a dependency it is not capitalized 'restangular' But when injected into your controller it is 'Restangular'

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

Let's code!

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

…

`

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 **RestangularProvider or Restangular service to change the global con

Issues· 253 open

View all issuesOpen on GitHub
  • #1326

    Bad error message for .get(null)

    waiting for PRUpdated Jul 19, 2022
  • #1220

    plain() method removes fields that was added to data in addResponseInterceptor

    Updated Aug 1, 2019
  • #1493

    Support DELETE method with body

    Updated Jul 18, 2019
  • #1492

    Deprecated?

    Updated Mar 4, 2019
  • #1453

    setPlainByDefault does not strip etag from responses

    bugwaiting for PRUpdated Jan 30, 2019
  • #1221

    `plain()` doesn't remove custom methods

    Updated Nov 5, 2018
  • #1490

    Multiple remove on Restangular

    Updated Aug 23, 2018
  • #612

    Dealing with single resources

    documentation upgradeUpdated Jul 23, 2018
  • #1442

    Tests fail with Angular 1.6

    waiting for PRUpdated Jul 6, 2018
  • #1480

    make etag a strong etag when used in if-None-Match

    Updated May 10, 2018

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言