1)我们首先需要引入ng-Resource 模块,在angular之后
<script src="js/vendor/angular.js"></script><script src="js/vendor/angular-resource.js"></script>
可以把User对象理解成同RESTful的后端服务进行交互的接口。
①GET请求: get(params,successFn,errrorFn)
不定义具体的参数,get()请求通常被用来获取单个资源。
//发送一个请求 with the body {name: ‘Ari‘}
User.save({},{name:‘Ari‘},function(resp){
$resource对常见的五种请求进行封装,我们还可以对$resource进行扩展,这里要扩展$resource我们需要传入第三个参数,该参数是一个对象。
$resource('/api/users',{},{ sendEmail:{ method:'', url:'', params:{}, isArray:boolean, transformRequest:函数或者函数数组 transformResponse:函数或者函数数组 cache:布尔型或缓存对象 timeout:数值或promise对象 withCredentials:布尔类型 responseType:字符串,用来设置XMLHttpRequestResponseType属性 } })
我们也可以将$resource服务当做自定义服务的基础。
angular.module(‘testApp‘, [‘ngResource‘]),factory(‘UserService‘,[‘$resource‘, function($resource){
return $resource(url,{},{});
}]);
define([ '{angular}/angular', '{angular-sanitize}/angular-sanitize', '{w20-ui}/modules/select' ], function (angular) { 'use strict'; var hubComponentImport = angular.module('hubComponentImport', ['ui.select', 'ngSanitize']); hubComponentImport.controller('hubImportComponentCtrl', ['$scope', '$routeParams', '$timeout','$resource','$location', function ($scope, $routeParams, $timeout,$resource,$location) { $scope.editComponent = function () { //初始化参数 $scope.component= {tags:[]}; //利用$resource自定义post方法 var Components= $resource('rest/components/url',{},{ getComponent: {method:'POST'} }); //接收页面输入数据,并发送请求 var component= Components.getComponent({},{url:$scope.componentUrl},function(data){ $scope.component = data; }); //转向edit.html页面 $scope.searchComponent = 'manage/views/edit.html'; }; }]); return { angularModules: [ 'hubComponentImport' ] }; });
原文地址:http://blog.csdn.net/u010834071/article/details/46360377