我想为 angularjs 中的所有后续 Http 调用编写单一方法
I want to Write a Single Method for all the Following Http Calls in angularjs
代码:-
AddressBook.service('AddressBookServices', ['$http', function ($http)
{
this.GetAll = function ()
{
return $http.get('api/contacts');
};
this.EditContact = function (newContact)
{
return $http.put('api/contacts', newContact);
};
this.AddContact=function(newContact)
{
return $http.post('api/contacts', newContact);
};
this.DeleteContact=function(Id)
{
return $http.delete('api/contacts', Id);
}
}]);
AddressBook.service('AddressBookServices', ['$http', function ($http)
{
this.ContactService = function (data, method) {
url = 'api/contacts';
if (method == 'delete') {
url += '/' + data;
data = "";
}
var req = {
method: method,
url: url,
data: data,
}
return $http(req);
};
并使用以下语法调用泛型方法
AddressBookServices.ContactService("sampledata","get");
最好的办法是使用资源而不是 http。资源将对它将执行的操作提供非常好的高级抽象。
代码:-
AddressBook.service('AddressBookServices', ['$http', function ($http)
{
this.GetAll = function ()
{
return $http.get('api/contacts');
};
this.EditContact = function (newContact)
{
return $http.put('api/contacts', newContact);
};
this.AddContact=function(newContact)
{
return $http.post('api/contacts', newContact);
};
this.DeleteContact=function(Id)
{
return $http.delete('api/contacts', Id);
}
}]);
AddressBook.service('AddressBookServices', ['$http', function ($http)
{
this.ContactService = function (data, method) {
url = 'api/contacts';
if (method == 'delete') {
url += '/' + data;
data = "";
}
var req = {
method: method,
url: url,
data: data,
}
return $http(req);
};
并使用以下语法调用泛型方法
AddressBookServices.ContactService("sampledata","get");
最好的办法是使用资源而不是 http。资源将对它将执行的操作提供非常好的高级抽象。