属性/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"
}

随机推荐

  1. MySQL生产库主从重新同步操作注意事项

    因为一些原因,我们会遇到生产主从库重新同步的时候.重新同步MYSQL主从的时候有有一些注意的地方. 从库还原前一定要记得reset,因为重启mysql并不影响复制进程,如果忘记reset,会导致你一边 ...

  2. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

  3. Sass:一种CSS预处理器语言

    http://sass-lang.com/ Sass是一种CSS预处理器语言,通过编程方式生成CSS代码.因为可编程,所以操控灵活性自由度高,方便实现一些直接编写CSS代码较困难的代码. 同时,因为S ...

  4. C#基础(五)——类中私有构造函数作用

    如果类成员有private修饰符,就不允许在类范围以外访问这个类成员.对类构造函数应用private修饰符时,则禁止外部类创建该类的实例.尽管看上去有些不好理解(既然不能实例化,那么这个类还有什么用处 ...

  5. MySQL查看和修改字符编码

    MySQL的默认编码是Latin1,不支持中文,要支持中午需要把数据库的默认编码修改为gbk或者utf8. 1.需要以root用户身份登陆才可以查看数据库编码方式(以root用户身份登陆的命令为:&g ...

  6. Intellij IDEA 14的注册码

    IntelliJ IDEA 14 注册码 IntelliJ IDEA 14 下载地址: IntelliJ IDEA 14 下载 分享几个license: (1) key:IDEA value:6115 ...

  7. 【WPF】路由事件

    总结WPF中的路由事件,我将学到的内容分为四部分来逐渐掌握 第一部分:wpf中内置的路由事件 以Button的Click事件来说明内置路由事件的使用 XAML代码: <Window x:Clas ...

  8. log4j配置文件写法

    ### direct log messages to stdout ###log4j.rootLogger=DEBUG,stdoutlog4j.appender.stdout=org.apache.l ...

  9. listview滚动时背景闪烁,背景黑或白问题解决

    android在使用listview时出现滚动时背景闪烁,变成背景黑或白的问题这样处理: 1:在布局文件中listview标签中加入: android:cacheColorHint="#00 ...

  10. Oracle本地,远程,分布式登录

    identify认证,确定; identity同一性,个性; 本地连接 sqlplus scott/tiger@localhost:1521/orcl 这句话就等于sqlplus scott/tige ...