AngularJS Best Practices: resource
2015-12-17 06:15
751 查看
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
A resource "class" object with methods for the default set of resource actions optionally extended with custom
Let's try to use that
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
ngResourcemodule 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> Email </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 ui-router 路由的简单介绍
- Angular2组件开发—调用服务(二)
- (笔记)angular 的根据后台StateCode本地显示指定文案
- Angular2组件开发—调用服务(一)
- Angular2组件开发—表单输入(五)
- AngularJS学习笔记
- 关于angularjs中,数据模型被改变,页面不刷新的解决办法
- Angular2组件开发—表单输入(四)
- Angular2组件开发—表单输入(三)
- angularjs
- Angular2组件开发—表单输入(二)
- 日常总结之angularjs的双向绑定全透析
- angularJS权威教程自动化测试笔记(一)
- Angular2组件开发—表单输入(一)
- Angular2组件开发—属性与事件(二)
- Angular2组件开发—属性与事件(一)
- 在 Angular 中实现搜索关键字高亮
- AngularJS进阶(二十一)Angularjs中scope与rootscope区别及联系
- AngularJS进阶(二十一)Angularjs中scope与rootscope区别及联系
- AngularJs parent index