AngularJS Best Practices: resource
A factory which creates a resource object that lets you interact with RESTful server-side data sources.
The returned resource object has action methods which provide high-level behaviors without the need to interact with the low level $http service.
Requires the ngResource
module to be installed.
A resource "class" object with methods for the default set of resource actions optionally extended with custom actions
. The default set contains these actions:
{
'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'}
}
Let's try to use that
_Layout.cshtml
<!DOCTYPE html>
<html ng-app="app">
<head>
<title></title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<base href="/">
</head>
<body>
<!--<ul>
<li>
<a href="#/users">Users</a>
</li>
<li>
<a href="#/roles">Roles</a>
</li>
</ul>--> <ul>
<li>
<a ui-sref="users">Users</a>
</li>
<li>
<a ui-sref="roles">Roles</a>
</li>
</ul> <ul>
<li>
<a href="/users">Users</a>
</li>
<li>
<a href="/roles">Roles</a>
</li>
</ul> @RenderBody() <script type="text/javascript" src="/Scripts/libs/angular/angular.min.js"></script>
<!--<script type="text/javascript" src="/Scripts/libs/angular/angular-route.min.js"></script>-->
<script type="text/javascript" src="/Scripts/libs/angular/angular-resource.min.js"></script>
<script type="text/javascript" src="/Scripts/libs/angular-ui/ui-router/angular-ui-router.min.js"></script>
<script type="text/javascript" src="/Scripts/app/app.js"></script>
<script type="text/javascript" src="/Scripts/app/components/users/app.users.js"></script>
<script type="text/javascript" src="/Scripts/app/components/users/app.users.routes.js"></script>
<script type="text/javascript" src="/Scripts/app/components/users/services/user.service.js"></script>
<script type="text/javascript" src="/Scripts/app/components/users/controllers/user.controller.js"></script>
<script type="text/javascript" src="/Scripts/app/components/roles/app.roles.js"></script>
<script type="text/javascript" src="/Scripts/app/components/roles/app.roles.routes.js"></script>
<script type="text/javascript" src="/Scripts/app/components/roles/controllers/role.controller.js"></script>
</body>
</html>
app.users.js
(function() {
'use strict'; angular
.module('app.users', [
//'ngRoute',
'ngResource',
"ui.router"
]); })();
user.service.js
(function() {
'use strict'; angular
.module('app.users')
.factory('UserService', ['$resource', function($resource) {
return $resource(('/api/users/:id'), { id: '@Id' }, {
list: { method: 'POST', url: '/api/users/list', isArray: true },
get: { method: 'GET' },
getNew: { method: 'GET', url: '/api/users/getnew' },
create: { method: 'POST' },
update: { method: 'PUT' },
delete: { method: 'DELETE' }
});
}]); })();
user.controller.js
(function() {
'use strict'; angular
.module('app.users')
.controller('UserController', ['$scope', 'UserService', function($scope, userService) {
$scope.users = userService.list();
}]); })();
user-list.tpl.html
<h2>Users</h2> <table>
<thead>
<tr>
<th>
First name
</th>
<th>
Last name
</th>
<th>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users">
<td>
{{user.FirstName}}
</td>
<td>
{{user.LastName}}
</td>
<td>
{{user.Email}}
</td>
</tr>
</tbody>
</table>
AngularJS Best Practices: resource的更多相关文章
- 黄聪:AngularJS中的$resource使用与Restful资源交互(转)
原文:http://blog.csdn.net/he90227/article/details/50525836 1.AngularJS中的 $resource 这个服务可以创建一个资源对象,我们可以 ...
- AngularJS中使用$resource
这个服务可以创建一个资源对象,我们可以用它非常方便地同支持RESTful的服务端数据源进行交互,当同支持RESTful的数据模型一起工作时,它就派上用场了. REST是Representat ...
- AngularJS Best Practices: SEO
Google can execute AJAX & JavaScript for indexing, you can read the below link for more detailed ...
- AngularJS Best Practices: pretty urls
By default, AngularJS will route URLs with a hashtag. For example: http://example.com/ http://exampl ...
- AngularJs练习Demo19 Resource
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- AngularJs练习Demo18 Resource
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- 浅谈AngularJS中使用$resource
这个服务可以创建一个资源对象,我们可以用它非常方便地同支持RESTful的服务端数据源进行交互,当同支持RESTful的数据模型一起工作时,它就派上用场了. REST是Representational ...
- AngularJS Best Practices: ng-include vs directive
For building an HTML template with reusable widgets like header, sidebar, footer, etc. Basically the ...
- AngularJS Best Practices: ui-router
ui-router is a 3rd-party module and is very powerful. It supports everything the normal ngRoute can ...
随机推荐
- Deep Learning模型之:CNN卷积神经网络(一)深度解析CNN
http://m.blog.csdn.net/blog/wu010555688/24487301 本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep le ...
- oracle触发器与数据导入导出的简单使用
exp cjtxx/123456@192.168.80.231/orcl file=d:\cjtxx.dmp owner=cjtxx [tables=tablename] imp cjttest/12 ...
- LINQ绑定List到GridView
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Trie URAL 7192 Chip Factory (15长春J)
题目传送门 题意:从n个数中选出不同的三个数a b c,使得(a+b)^c 最大 分析:先将所有数字按位插入到字典树上,然后删除两个数字,贪心询问与剩下的数字最大异或值. /************* ...
- java-集合3
浏览以下内容前,请点击并阅读 声明 Queue接口(队列) 需要对一些列的元素进行处理前,我们可以把他们放到Queue对象中,除了继承Collection接口的方法外,队列还有一些插入,删除和检查操作 ...
- 【转】java-String中的 intern()
转自:http://blog.sina.com.cn/s/blog_69dcd5ed0101171h.html 1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值 ...
- Leetcode Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Python中输出格式化的字符串
在Python中,采用的格式化方式和C语言是一致的,用%实现,举例如下: >>> 'Hello, %s' % 'world' 'Hello, world' >>> ...
- TEST===>Sqlserver中获取年月日时分秒
可以用两种方法获取 1. select GETDATE() as '当前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) ...
- javascript平时小例子⑦(鼠标跟随的div)
<!doctype html><html> <head> <meta charset="utf-8"> <title>无 ...