$resource是一个更方便地与RESTful服务器API进行交互,可以方便地定义一个REST资源,而不必手动所有的声明CRUD方法的Service。

使用

1.要使用$resource首先要在HTML中引入它的独立的js文件;

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular-resource.js"></script>

2.在app中添加对应的依赖注入;

var app = angular.module('helloApp, ['ngResource']);

3.为资源建立一个factory;

app.factory('Notes',['$resource', function(){
return $resource('/notes/:id');
}]);

注:当然,你也可以不把$esource的实例放到Factory里,直接在控制器中存起来:var Notes = $resource('/notes/:id)

4.进行增删改查(CRUD)的操作

app.controller('NotesCtrl',['$scope','Notes',function(){
var notes = Notes.query(function(){
//GET or POST code
}); var note = new Notes({content:'xxx'});
}]);

$resource提供了五种默认操作:get, query, save, remove, delete。你可以配置一个update操作来完成HTTP PUT。

app.factory('Notes', ['$resource', function($resource){
return $resource('/notes/:id', null, {
update:{method:'PUT'}
});
}]);

在创建控制器时引入ngResource

var helloApp = angular.module("helloApp", [ 'ngResource' ]);
helloApp.controller("HttpController", [ '$scope', '$resource', function($scope, $resource) {
//code
} ]);

用POST方法来提交表单信息

var helloApp = angular.module("helloApp", [ 'ngResource' ]);
helloApp.controller("HttpController", [ '$scope', '$resource', function($scope, $resource) {
$scope.users = [
{
'firstname': 'Ajitesh',
'lastname': 'Shukla',
'address': 'Hyderbad',
'email': 'ajitesh101@gmail.com'
},
{
'firstname':'Sumit',
'lastname':'Jha',
'address':'Muzaffarpur',
'email':'sumitjha2011@yahoo.com'
},
]; $scope.saveUser = function(){
// 创建一个resource对象
var User = $resource('/user/new'); // 调用save方法
//(其实和我们$http.post(url,data).success(function(){})是一样的,只是它封装一下而已)
User.save(
{
firstname:$scope.firstname,
lastname:$scope.lastname,
address:$scope.address,
email:$scope.email
},
function(response){
$scope.message = response.message;
}
);
};
}]);

angularJS $resource的更多相关文章

  1. Spring resource bundle多语言,单引号format异常

    Spring resource bundle多语言,单引号format异常 前言 十一假期被通知出现大bug,然后发现是多语言翻译问题.法语中有很多单引号,单引号在format的时候出现无法匹配问题. ...

  2. Spring5:@Autowired注解、@Resource注解和@Service注解

    什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...

  3. 【初探Spring】------Spring IOC(三):初始化过程---Resource定位

    我们知道Spring的IoC起到了一个容器的作用,其中装得都是各种各样的Bean.同时在我们刚刚开始学习Spring的时候都是通过xml文件来定义Bean,Spring会某种方式加载这些xml文件,然 ...

  4. 2000条你应知的WPF小姿势 基础篇<34-39 Unhandled Exceptions和Resource>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. 【解决方案】 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userHandler': Injection of resource dependencies failed;

    一个错误会浪费好多青春绳命 鉴于此,为了不让大家也走弯路,分享解决方案. [错误代码提示] StandardWrapper.Throwableorg.springframework.beans.fac ...

  6. AngularJS Resource:与 RESTful API 交互

    REST(表征性状态传输,Representational State Transfer)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格.RESTful风格的设计不仅 ...

  7. 运行nltk示例 Resource u'tokenizers punkt english.pickle' not found解决

    nltk安装完毕后,编写如下示例程序并运行,报Resource u'tokenizers/punkt/english.pickle' not found错误 import nltk sentence ...

  8. Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解

    方法inflate(int resource, ViewGroup root, boolean attachToRoot) 中 第一个参数传入布局的资源ID,生成fragment视图,第二个参数是视图 ...

  9. ORA-00054: resource busy and acquire with NOWAIT specified

    删除表时遇到 ORA-00054:资源正忙,要求指定NOWAIT 错误.以前在灾备中心遇到过. 资源被锁定了,没有办法删除. 报错日志:ORA-00054: resource busy and acq ...

  10. angular学习笔记(二十八-附1)-$resource中的资源的方法

    通过$resource获取到的资源,或者是通过$resource实例化的资源,资源本身就拥有了一些方法,$save,$delete,$remove,可以直接调用来保存该资源: 比如有一个$resour ...

随机推荐

  1. 评论alpha发布

    第一组:新蜂小组 题目:俄罗斯方块 评论:很荣幸被邀请体验此游戏,感到加速下落时不是很灵敏,没有计分,页面不够美观(据说还在改善),整体框架已完成,基本功能已具备. 第二组:天天向上 题目:连连看 评 ...

  2. linux使用密文生成os账户

    1.生成账户 [hufangrui@xxx ~]$ openssl passwd -1Password: Verifying - Password: $1$szzkROBZ$GYxffRLp8K5vW ...

  3. 【问底】王帅:深入PHP内核(一)——弱类型变量原理探究

    来源:CSDN    http://www.csdn.net/article/2014-09-15/2821685-exploring-of-the-php 作者:王帅 摘要:PHP作为一门简单而强大 ...

  4. Read N Characters Given Read4 II - Call multiple times

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  5. Thread start()方法和run()方法的区别

    转自:http://www.cnblogs.com/skywang12345/p/3479083.html start():作用一个新的线程,新线程会执行相应的run()方法,start()不能被重复 ...

  6. p2 碰撞

    P2可以实现物体碰撞模拟,同时在碰撞过程中派发一些事件实现碰撞检测,将碰撞信息及时反馈,以添加相应的特效. P2中,当两个刚体的最小包围盒AABB发生重叠,碰撞就开始了:然后刚体的形状发生重叠,同时P ...

  7. Embarcadero Delphi 7 Enterprise 7.0.4.453 中文版

    在 win7下可能报错请大家按下面的方式 delphi7运行不正常的提示unable to rename'c:\program files\Borland\delphi7\Bin\delphi32.$ ...

  8. Mybatis笔记六:Mybatis中SqlSessionFactoryBuilder/SqlSessionFactory/SqlSession/映射器实例的作用域(Scope)和生命周期

    SqlSessionFactoryBuilder 这个类可以被实例化.使用和丢弃,一旦创建了 SqlSessionFactory,就不再需要它了.因此 SqlSessionFactoryBuilder ...

  9. Alpha 冲刺 —— 十分之五

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助测试的进行 测试项目运行的服务器环境 ...

  10. 【bzoj3195】 Jxoi2012—奇怪的道路

    http://www.lydsy.com/JudgeOnline/problem.php?id=3195 (题目链接) 题意 一张$n$个点$m$条边的无向图,每个点度数为偶数,一个点只能向标号与它的 ...