问题描述
我是相当新的,我不能够找到任何文件或实施例此。我所希望做的是扩大基本服务,这样我可以使用其他服务的基本服务下定义的方法。因此,例如说我有一个基本的服务如下:
angular.module('myServices',[])。 工厂('BasicService',函数($ HTTP){
VAR some_arg =ABCD
VAR BasicService = {
method_one:功能(ARG = some_arg){/ * code的方法一* /},
method_two:功能(ARG = some_arg){/ * code的方法二* /},
method_three:功能(ARG = some_arg){/ * code的方法三* /},
});
返回BasicService;
}
);
现在我想要定义从上面延伸 BasicService
,这样我可以使用从我的扩展服务的BasicService下定义的方法扩展服务。也许是这样的:
厂('ExtendedService',函数($ HTTP){
变种ExtendedService = BasicService();
ExtendedService ['method_four'] =功能(){/ * code的方法四* /}
返回ExtendedService;
}
您 ExtendedService
应注入 BasicService
中为了能够访问它。在那旁边 BasicService
是一个对象字面,所以你不能真正把它作为函数( BasicService()
)。
.factory('ExtendedService',函数($ HTTP,BasicService){
BasicService ['method_four'] =函数(){};
返回BasicService;
}
I am fairly new to angularjs
and am not able to find any documentation or examples for this. What I am looking to do is to extend a basic service so that i can use the methods defined under the basic service from other services. So for example say i have a basic service as follows.
angular.module('myServices', []).
factory('BasicService', function($http){
var some_arg = 'abcd'
var BasicService = {
method_one: function(arg=some_arg){ /*code for method one*/},
method_two: function(arg=some_arg){ /*code for method two*/},
method_three: function(arg=some_arg){ /*code for method three*/},
});
return BasicService;
}
);
Now i want to define an Extended service that extends from the above BasicService
so that i can use methods defined under the BasicService from my extended service. Maybe something like:
factory('ExtendedService', function($http){
var ExtendedService = BasicService();
ExtendedService['method_four'] = function(){/* code for method four */}
return ExtendedService;
}
Your ExtendedService
should inject the BasicService
in order to be able to access it. Beside that BasicService
is an object literal, so you can't actually call it as function (BasicService()
).
.factory('ExtendedService', function($http, BasicService){
BasicService['method_four'] = function(){};
return BasicService;
}
这篇关于我如何能延伸服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!