을 사용하는 우리는 $resource 갈 수있는 좋은 방법으로 찾을 수 있습니다. $httpBackend 서비스를 사용하면 손쉽게 테스트 할 수 있습니다. 우리에게는 다음과 같은 것이 있습니다. 그것은 우리를 위해 잘 작동합니다. 조금 더 제어하려는 경우 언제든지 $http 서비스로 되돌아 갈 수 있습니다.
보기
<!DOCTYPE html >
<html ng-app="myApp">
<head>
</head>
<body ng-controller="CustomerController">
<form name="form" novalidate>
<input type="text" ng-model="customer.name" required />
<input type="text" ng-model="customer.address" required />
<button ng-click="add(customer)">Save</button>
</form>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular-resource.js"></script>
<script src="~/Scripts/app/app.js"></script>
<script src="~/Scripts/app/services/customerResource.js"></script>
<script src="~/Scripts/app/controllers/CustomerController.js"></script>
</body>
</html>
서비스 :
myApp.factory('customerResource', function($resource){
var resource = $resource('/data/customer/:id', { id: '@id' }, { 'put' : {method:'PUT' } });
return {
getAll : function(){
return resource.query();
},
add : function(customer){
return resource.save(customer);
},
update : function(customer){
return resource.put({ id: customer._id }, customer);
},
remove : function(id){
return resource.remove({ id: id });
}
};
});
컨트롤러 :
myApp.controller('CustomerController', function($scope, customerResource) {
$scope.customer = {};
$scope.customers = customerResource.getAll();
$scope.add = function(customer){
$scope.customers.push(customerResource.add(customer));
}
$scope.update = function(customer){
customerResource.update(customer);
}
$scope.remove = function(customer){
customerResource.remove(customer._id);
$scope.customers.splice($scope.customers.indexOf(customer), 1);
}
});
아주 기본적인 테스트 :
describe('customerResource', function(){
beforeEach(module('myApp'));
describe('getAll', function(){
it('should issue a GET request to /data/customer', inject(function(customerResource, $httpBackend){
$httpBackend.expectGET('/data/customer').respond([{ level: '5'}]);
var customers = customerResource.getAll();
$httpBackend.flush();
expect(customers[0].level).toBe('5');
}));
it('should return an array of custoemrs', inject(function(customerResource, $httpBackend){
$httpBackend.when('GET', '/data/customer').respond([{ level: '5'}, { level: '6'}]);
var customers = customerResource.getAll();
$httpBackend.flush();
expect(customers[0].level).toBe('5');
expect(customers[1].level).toBe('6');
}));
});
MVC 액션는 (ADD - MVC 모델 바인더는 VM에 html로 PARAMS 구문 분석 작업 할 것) :
[HttpPost]
public ActionResult Customer(Customer customer)
{
// add the customer to the database or whatever
}
뷰 모델을 :
public class Customer
{
public string Name { get; set; }
public string Address { get; set; }
}
HTTP 요청은 다음과 같습니다.
Request URL:http://mywebsite/data/customer
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en;q=0.8,en-US;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Content-Length:30
Content-Type:application/json;charset=UTF-8
Host:mywebsite
Origin:http://mywebsite
Pragma:no-cache
Request Payloadview source
{name:somename, address:someaddress}
address: "somename"
name: "someaddress"
HTH
$ rest를 기반으로 $ rest를 사용해야합니다. http://docs.angularjs.org/api/ngResource.$resource –
가능한 [서버에서 데이터를 가져 오는 권장 방법] (http://stackoverflow.com)/questions/11850025/recommended-way-of-the-server-from-the-server) – Stewie
@Stewie 중복 된 것처럼 보이지만, 나는'$ resource'의 사용에 신경 쓰지 않습니다. – Martijn