var customersApp = angular.module('customersApp', ['ngGrid']);
var url = 'api/Customer';
//the factory object for the webAPI call.
customersApp.factory('customerRepository', function ($http) {
return {
insertUser: function (callback,user) {
var user = { "id": user.id, "city": user.city, "name": user.name, "address": user.address, "contactNo": user.contactNo, "emailId": user.emailId };
$http.post(url, user).success(callback);
}
});
控制器
customersApp.controller('customerCtrl', function ($scope, customerRepository) {
customerRepository.insertUser(function () {
alert('customer inserted successfully');
}, $scope.New)
});
我要做的是将工厂中的$ http更改为$资源
customersApp.factory('customerRepository', function($resource){
return
$resource(url, { id: '@id' }, { update: { method: 'POST', } })
}
我的问题是当我从$ http更改为$ resource时,我将" user"正在使用的工厂insertUser中的数据。
请在工厂的$资源中告诉我我在哪里插入"用户"数据。谢谢
答案 0 :(得分:0)
你可以这样做:
资源工厂创建对象并将对象实例数据用作POST
customersApp.factory("customerRepository", function($resource) {
return $resource("http://someUrl", {}, {
update: {method : "POST"}
});
});
在您的控制器中
$scope.data = {}; // your data initialization stuff
$scope.data.someDataKey = 'someDataValue'; // add your default data
var customerrepository = new customerRepository($scope.data);
customerrepository.$update();
答案 1 :(得分:0)
这是一个例子
customersApp.controller('customerCtrl', function ($scope, $resource) {
var customerRepository = $resource(url)
customerRepository.save($scope.New, function () {
alert('customer inserted successfully');
})
});
这是使用$ resouce的默认资源操作save
,定义为here
non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
non-GET instance actions: instance.$action([parameters], [success], [error])
如果使用实例操作,则在使用new