angularjs - How to inherit from base provider (not the provider factory)? -
say have base provider:
angular.module('app').provider('baseclient', function () { this.setsomething = function (something) { // store `something` somewhere return this; }; }); and these 2 other sub-providers:
angular.module('app').provider('clienta', function () { this.$get = function () { return { foo: function () { console.log('foo', /* read `something`, needs output 'aaa' */); } } }; }); angular.module('app').provider('clientb', function () { this.$get = function () { return { bar: function () { console.log('bar', /* read `something`, needs output 'bbb' */); } } }; }); angular.module('app').config(function (clientaprovider, clientbprovider) { clientaprovider.setsomething('aaa'); clientbprovider.setsomething('bbb'); }); how make clienta , clientb inherit provider section of baseclient in such way can call clientaprovider.setsomething('aaa') , clientbprovider.setsomething('bbb') , store value per provider, while using same setsomething implementation?
i have bunch of these providers (more these 2) provider section same, configuration implementation same factory section of providers different.
thoughts?
you inject baseclientprovider clienta provider.
full code here 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); } } }; }]);
Comments
Post a Comment