Restangular的使用

本文详细介绍了如何使用Restangular库简化AngularJS应用程序中与RESTful API的交互过程。包括创建Restangular对象、获取资源列表、POST操作、GET特定资源、使用Promise进行链式调用、更新资源等常见操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// First way of creating a Restangular object. Just saying the base URL

var baseAccounts = Restangular.all('accounts');


// This will query /accounts and return a promise.

baseAccounts.getList().then(function(accounts) {

  $scope.allAccounts = accounts;

});


// Does a GET to /accounts

// Returns an empty array by default. Once a value is returned from the server

// that array is filled with those values. So you can use this in your template

$scope.accounts = Restangular.all('accounts').getList().$object;


var newAccount = {name: "Gonto's account"};


// POST /accounts

baseAccounts.post(newAccount);


// GET to http://www.google.com/ You set the URL in this case

Restangular.allUrl('googlers', 'http://www.google.com/').getList();


// GET to http://www.google.com/1 You set the URL in this case

Restangular.oneUrl('googlers', 'http://www.google.com/1').get();


// You can do RequestLess "connections" if you need as well


// Just ONE GET to /accounts/123/buildings/456

Restangular.one('accounts', 123).one('buildings', 456).get()


// Just ONE GET to /accounts/123/buildings

Restangular.one('accounts', 123).getList('buildings')


// Here we use Promises then

// GET /accounts

baseAccounts.getList().then(function (accounts) {

  // Here we can continue fetching the tree :).


  var firstAccount = accounts[0];

  // This will query /accounts/123/buildings considering 123 is the id of the firstAccount

  $scope.buildings = firstAccount.getList("buildings");


  // GET /accounts/123/places?query=param with request header: x-user:mgonto

  $scope.loggedInPlaces = firstAccount.getList("places", {query: param}, {'x-user': 'mgonto'})


  // This is a regular JS object, we can change anything we want :)

  firstAccount.name = "Gonto"


  // If we wanted to keep the original as it is, we can copy it to a new element

  var editFirstAccount = Restangular.copy(firstAccount);

  editFirstAccount.name = "New Name";



  // PUT /accounts/123. The name of this account will be changed from now on

  firstAccount.put();

  editFirstAccount.put();


  // PUT /accounts/123. Save will do POST or PUT accordingly

  firstAccount.save();


  // DELETE /accounts/123 We don't have first account anymore :(

  firstAccount.remove();


  var myBuilding = {

    name: "Gonto's Building",

    place: "Argentina"

  };


  // POST /accounts/123/buildings with MyBuilding information

  firstAccount.post("Buildings", myBuilding).then(function() {

    console.log("Object saved OK");

  }, function() {

    console.log("There was an error saving");

  });


  // GET /accounts/123/users?query=params

  firstAccount.getList("users", {query: params}).then(function(users) {

    // Instead of posting nested element, a collection can post to itself

    // POST /accounts/123/users

    users.post({userName: 'unknown'});


    // Custom methods are available now :).

    // GET /accounts/123/users/messages?param=myParam

    users.customGET("messages", {param: "myParam"})


    var firstUser = users[0];


    // GET /accounts/123/users/456. Just in case we want to update one user :)

    $scope.userFromServer = firstUser.get();


    // ALL http methods are available :)

    // HEAD /accounts/123/users/456

    firstUser.head()


  });


}, function errorCallback() {

  alert("Oops error from server :(");

})


// Second way of creating Restangular object. URL and ID :)

var account = Restangular.one("accounts", 123);


// GET /accounts/123?single=true

$scope.account = account.get({single: true});


// POST /accounts/123/messages?param=myParam with the body of name: "My Message"

account.customPOST({name: "My Message"}, "messages", {param: "myParam"}, {})


转载于:https://my.oschina.net/u/1453451/blog/504487

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值