Restangular的使用
2019独角兽企业重金招聘Python工程师标准>>>
// 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
Restangular的使用相关推荐
- Traveller项目介绍
Traveller,翻译为旅行家,是我用来实践最佳web技术的项目,主题是一个给旅行爱好者提供旅行信息的网站. 目标是组合现最流行的web技术,实现符合中国用户使用习惯的网站. 相关网址 Git:ht ...
- angularjs http和ajax,AngularJS $ http和$ resource
$http使得通用的AJAX调用,其中一般意味着它可以包括基于REST的 API加上非RESTful API. 并且$resource专门针对该RESTful部分. 近年来,Restful Api盛行 ...
- angular的html引入js,在AngularJS中的文件夹中加载JavaScript和CSS文件
AngularJS不支持你想要的东西,但是你可以看看Grunt或Gulp等构建工具,它们可以让你"构建"你的应用程序.在您的情况下,这些工具可以查找CSS文件并将它们连接成一个文件 ...
- 前端实习日记——高新兴科技集团
7月16日(实习的第一天) 看公司前端的代码规范:了解AngularJS:了解Less:看项目代码. 项目"明镜"介绍:一两年前就开始做了,是做公安机关的生意,需求是实现公安执法办 ...
- 25个超有用的 AngularJS Web 开发工具
AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,改善了JavaScript. 下面我要说的就是25个超有用的AngularJS工具, ...
- AngularJS 开发辅助工具
AngularJS 是一种新 JavaScript 框架,目的是降低要求,加快开发速度.AngularJS 是让 HTML 标记动态,使其对 web 开发者更有帮助,同时从大量的分段中给定一个标准结构 ...
- 前端架构,前端架构组织结构,目录结构架构
[编者按]本文作者Matt Carella为我们讲解了如何构建一个良好的架构.除了选择正确的工具以外,在组织工作流中遵循清晰整洁的原则对于我们来说是非常重要的.或许有的人会觉得文中所说的是在DOS时代 ...
- 《AngularJS深度剖析与最佳实践》一1.3 创建项目
本节书摘来自华章出版社<AngularJS深度剖析与最佳实践>一书中的第1章,第1.3节,作者 雪狼 破狼 彭洪伟,更多章节内容可以访问云栖社区"华章计算机"公众号查看 ...
- 你想了解的前端知识都在这里了
前端开发全面知识库,包括HTML5,CSS3和js的基本框架知识,以及DOM和BOM操作的基础知识和一些基本工具和IDE.学习本篇,你将了解到以下知识. 核心 Core HTML5 W3C http: ...
最新文章
- 【Zookeeper系列】ZooKeeper管理分布式环境中的数据(转)
- keil 器件是空的,Keil的Device为空,不能选择器件
- C++中int与string的相互转换
- Redis中的发布与订阅的概念与以命令行的方式实现发布订阅举例
- Mat转CImage
- centos 安装配置ftp服务器
- jquery之ajax请求工具类
- electron打包现成的vue项目成.exe文件,build打包vue项目(quasar 框架)
- Asp.Net Core + Docker 搭建
- springboot整合Mybatis提示org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
- 设置go path_Go命令的PATH安全性
- Castle动态代理拦截
- 网上抢购茅台催生黄牛党:必须严打各类抢购软件
- java/php/net/python职工请假管理系统设计
- 【洛谷试炼场】普及练习场——排序
- matlab分布函数逆函数,正态分布函数的反函数求导 matlab
- Struts2中的OGNL表达式的详解
- ethercat通讯移植
- Python3,5行代码,Chatxxx能对PDF文件进行旋转、提取、合并等一系列操作,看了这篇,80岁老奶奶走路都不扶墙了。
- 蓝桥青少竞赛考纲,一文须知【Python 篇】
热门文章
- 三心二意,助你好运?
- POS时机未到,POW强攻是实现全球货币的正确道路
- Python安装、使用MySQL数据库
- LazyT 提供对延迟初始化的支持
- 创业感悟:技术兄弟为什么一直没有起来(1)
- FileReader/FileWriter复制文件
- Effective C++ .07 virtual析构函数的提供
- 访问被拒绝:“microsoft.web.ui.webcontrols”的解决办法
- js高级—tab栏切换(面向对象做法)
- 【PHP】xampp配置多个监听端口和不同的网站目录(转)