$resource
属性/URL映射
AngularJS Resource:与 RESTful API 交互
自定义$resource方法


<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div ng-controller="controller">
<button ng-click="get()">get</button>
<button ng-click="query()">query</button>
<button ng-click="save()">save</button>
<button ng-click="remove()">remove</button>
<button ng-click="sendEmail()">sendEmail</button>
</div>
<script src="angular.js"></script>
<script src="angular-resource.js"></script>
<script>
angular.module('app', ['ngResource']).controller('controller', ['$scope', 'Game', function($scope, Game) {
$scope.get = function() {
// data1.json?group=1&id=id
// GET
Game.get({
gameId: 'data1',
id: 'id'
}, function(resp) {
console.log(resp) resp.name = 'jiji3'
// data2.json?group=1
// POST
// {id: "data2", name: "jiji3"}
resp.$save()
}, function(err) {
console.log(err)
})
} $scope.query = function() {
// data1.json?group=1&id=id
// GET
Game.query({
gameId: 'data1',
id: 'id'
})
} $scope.save = function() {
// data1.json?group=1&id=id
// POST
// {name: "Ari"}
Game.save({
gameId: 'data1',
id: 'id'
}, {
name: 'Ari'
})
} $scope.remove = function() {
Game.remove({}, {
gameId: 'data1',
id: 'data2'
})
/*
{
gameId: 'data1',
id: 'data2'
} 这2个{}是数据 所以匹配@id
*/
} $scope.sendEmail = function() {
Game.sendEmail({
id: 'data1'
})
}
}]).factory('Game', ['$resource', function($resource) {
/**
* $resource(url[, paramDefaults][, actions]);
* If the parameter value is prefixed with @ then the value of that parameter is extracted from the data object
* (useful for non-GET operations).
*/
return $resource('/test/test/:gameId.json', {
gameId: '@id',
group: '1'
}, {
sendEmail: {
method: 'POST'
}
})
}])
</script>
</body>
</html>
data1.json
{
"id": "data2",
"name": "jiji1"
}
data2.json
{
"id": "data1",
"name": "jiji2"
}
随机推荐
- 安卓热更新之Nuwa实现步骤
安卓热更新之Nuwa实现步骤 最近热更新热修复的功能在安卓应用上越发火热,终于我的产品也提出了相应的需求. 经过两天的研究,搞定了这个功能,在这里还要多谢大神们的博客,大神们的原理分析很到位,不过对于 ...
- linux系统制作简单流程
制作嵌入式平台使用的Linux内 核, 方法和制作PC平台 的Linux内 核基本一致, 下面使用 对比的方式介绍如何制作用 于6410开发板的内 核. 1. 清除原有配置与中间文件x86: make ...
- Python3 IO
在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开一个文件对象(通常称为文件描述符),然后,通过操作系统提供的接口从这个文件对象中 ...
- DataGridView几个基本属性
DataGridView 经常用到,但是很多东西都不熟悉,以至于总去上网查,这次我整理一下,全部都记下来,再用就方便了. 1.禁止用户新建行,就是去掉最后那个行标题上带星号的那个行 dataGridV ...
- div重叠不变形
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- ORA-12012 error on auto execute of job 8887
*** ACTION NAME:(AUTO_SPACE_ADVISOR_JOB) -- ::58.046 *** MODULE NAME:(DBMS_SCHEDULER) -- ::58.046 ** ...
- tableView -- tips
1. 如果发现TableView的第一个sectionHeader不显示, 那么可以断定, 你没有用代理方法来设置 sectionHeader的高度! #pragma mark - delegate ...
- hdu 2665 Kth number(划分树模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ] 改变一下输入就可以过 http://poj.org/problem? ...
- Spring 数据源配置一:单一数据源
最近遇到一个项目,需要访问都多个数据源,并且数据库是不同厂商(mysql, sqlserver). 所以对此做了一些研究,这里咱们采用渐进的方式来展开,先谈谈单一数据源配置.(稍后有时间会陆续补充其 ...
- Linux PS 命令详解
Linux操作系统PS命令详细解析 要对系统中进程进行监测控制,用 ps 命令满足你. /bin/ps ps 是显示瞬间行程的状态,并不动态连续:如果想对进程运行时间监控,应该用 top 工具. ki ...