, $resource, .
, , , GET, PUT, DELETE & UPDATE
Factory
var app = angular.module('mainApp',['ngResource']);
app.factory('Customer', function($resource) {
return $resource('/api/customers/:id');
});
app.controller('CustomerController', function($scope, Customer) {
var customer = Customer.get({ id: $scope.id }, function() {
console.log(customer);
});
var customers = Customer.query(function() {
console.log(customers);
});
$scope.customer = new Customer();
$scope.customer.data = 'some data';
Customer.save($scope.customer, function() {
});
});
, . $resource REST API
$http , customerFactory, , . angular.constant, , , .
Constant
app.constant('customerMethods', [
{name: 'create', type: 'post', url: 'api/customers/1', dataParam:{ customer: customer }},
{name: 'read', type: 'get', url: 'api/customers', dataParam:{ params: { uri : customer.selfUri }},
{name: 'create', type: 'post', url: 'api/customers/1, dataParam:{ customer: customer }}, //you need to provide customer object
{name: 'create', type: 'post', 'api/customers/1', dataParam: { customer: customer }},//you need to provide customer object
]);
Factory
app.factory('customersFactory', ['$http', 'customerMethods', 'customer',
function($http, customerMethods, customer) {
var customersFactory = {};
var customers = {};
var customerMethods = customerMethods;
angular.forEach(customerMethods, function(val, index) {
angular.extend(customersFactory, {
val.name: function() {
return $http[val.type](val.url, val.dataParam);
}
});
});
return customersFactory;
}
]);
. , , .
, . .