我正在尝试提供扩展Angular $ http服务的代码,以创建更多面向业务的服务。
这是我的代码:
基本服务:
classes = angular.module("app", []);
classes.factory('baseService', function ($http, $q) {
var apiUrl = "http://localhost/services/";
// instantiate initial object
var baseService = function () {
};
baseService.prototype.execute = function (webService, method, params) {
params = typeof params !== "undefined" ? params : {};
var deferred = $q.defer();
var response = $http({
method: "post",
dataType: "json",
data: JSON.stringify(params),
headers: {
'Content-Type': "application/json"
},
url: apiUrl + webService + "/" + method
});
response.success(function (data) {
deferred.resolve(data);
});
response.error(function (data) {
alert('Error');
});
// Return the promise to the controller
return deferred.promise;
};
return baseService;
});
扩展服务:
home.factory("homeService", function (baseService) {
// create our new custom object that reuse the original object constructor
var homeService = function () {
baseService.apply(this, arguments);
};
// reuse the original object prototype
homeService.prototype = new baseService();
return homeService;
});
控制器:
.....
homeService.prototype.execute("reports.asmx", "getWebReports").then(callback);
一切正常,但我希望在我的扩展服务中有一个内部函数来从控制器中调用它。另外,我可能在扩展服务中有多个功能。