如何从基地提供者(而不是供应商工厂)继承? [英] How to inherit from base provider (not the provider factory)?
问题描述
说我有这个基地提供者:
Say I have this base provider:
angular.module('app').provider('BaseClient', function () {
this.setSomething = function (something) {
// Store `something` somewhere
return this;
};
});
而现在这2个其他子供应商:
And now these 2 other sub-providers:
angular.module('app').provider('ClientA', function () {
this.$get = function () {
return {
foo: function () {
console.log('FOO', /* Read `something`, needs to output 'aaa' */);
}
}
};
});
angular.module('app').provider('ClientB', function () {
this.$get = function () {
return {
bar: function () {
console.log('BAR', /* Read `something`, needs to output 'bbb' */);
}
}
};
});
angular.module('app').config(function (clientAProvider, clientBProvider) {
clientAProvider.setSomething('aaa');
clientBProvider.setSomething('bbb');
});
如何让客户端A
和 ClientB
继承的提供者部分 BaseClient
以这样的方式,我可以打电话 clientAProvider.setSomething('AAA')
和 clientBProvider.setSomething('BBB')
并存储每个供应商的价值,而使用相同的 setSomething
实施?
How do I make ClientA
and ClientB
inherit the provider section of BaseClient
in such a way that I can call clientAProvider.setSomething('aaa')
and clientBProvider.setSomething('bbb')
and store that value per provider, while using the same setSomething
implementation?
我有一堆这些提供商(超过这2),其中提供者部分始终是相同的,该配置的实现是始终不变的,但这些供应商的工厂部分是不同的。
I have a bunch of these providers (more than these 2) where the provider section is always the same, the configuration implementation is always the same but the factory section of those providers are different.
思考?
推荐答案
您可以注入 BaseClientProvider
到客户端A
供应商。
全部code是这里 plnkr
app.provider('BaseClient', function() {
this.config = {
something: null
};
this.setSomething = function(something) {
this.config.something = something;
return this;
};
this.$get = function() {};
});
app.provider('ClientA', ['BaseClientProvider', function(BaseClientProvider) {
var self = this;
angular.extend(self, BaseClientProvider);
self.$get = function() {
return {
foo: function() {
console.log('FOO', self.config.something);
}
}
};
}]);
这篇关于如何从基地提供者(而不是供应商工厂)继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!