如何从基础提供者(不是提供者工厂)继承?

时间:2015-06-18 16:51:04

标签: angularjs prototypal-inheritance angularjs-provider

说我有这个基础提供者:

angular.module('app').provider('BaseClient', function () {
    this.setSomething = function (something) {
        // Store `something` somewhere
        return this;
    };
});

现在这两个其他子提供商:

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');
});

如何让ClientAClientB继承BaseClient的提供者部分,以便我可以调用clientAProvider.setSomething('aaa')clientBProvider.setSomething('bbb')并存储每个提供商的价值,同时使用相同的setSomething实施?

我有一堆这些提供商(超过这两个),其中提供者部分始终相同,配置实现始终相同,但这些提供者的工厂部分是不同的。

思想?

1 个答案:

答案 0 :(得分:1)

您可以将> Error: [$injector:modulerr] > http://errors.angularjs.org/1.3.7/$injector/modulerr?p0=inspinia&p1=%5B%24injector%3Aunpr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.3.7%2F%24injector%2Funpr%3Fp0%3DadalProvider%0AT%2F%3C%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A6%3A416%0AOb%2Fn.%24injector%3C%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A38%3A307%0Ad%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A36%3A308%0Ae%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A37%3A64%0Ad%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A35%3A293%0Ag%2F%3C%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A35%3A425%0As%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A7%3A300%0Ag%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A35%3A202%0AOb%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A38%3A435%0Asc%2Fd%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A17%3A350%0Asc%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A18%3A153%0AJd%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A16%3A483%0A%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A249%3A412%0An.Callbacks%2Fj%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fjquery%2Fjquery-2.1.1.min.js%3A2%3A26855%0An.Callbacks%2Fk.fireWith%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fjquery%2Fjquery-2.1.1.min.js%3A2%3A27673%0A.ready%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fjquery%2Fjquery-2.1.1.min.js%3A2%3A29465%0AI%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fjquery%2Fjquery-2.1.1.min.js%3A2%3A29656%0A%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fapp.js%3A15%3A6%0A > angular.min.js:6:415 Error: [$injector:modulerr] > http://errors.angularjs.org/1.3.7/$injector/modulerr?p0=inspinia&p1=%5B%24injector%3Aunpr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.3.7%2F%24injector%2Funpr%3Fp0%3DadalProvider%0AT%2F%3C%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A6%3A416%0AOb%2Fn.%24injector%3C%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A38%3A307%0Ad%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A36%3A308%0Ae%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A37%3A64%0Ad%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A35%3A293%0Ag%2F%3C%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A35%3A425%0As%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A7%3A300%0Ag%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A35%3A202%0AOb%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A38%3A435%0Asc%2Fd%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A17%3A350%0Asc%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A18%3A153%0AJd%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A16%3A483%0A%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fangular%2Fangular.min.js%3A249%3A412%0An.Callbacks%2Fj%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fjquery%2Fjquery-2.1.1.min.js%3A2%3A26855%0An.Callbacks%2Fk.fireWith%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fjquery%2Fjquery-2.1.1.min.js%3A2%3A27673%0A.ready%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fjquery%2Fjquery-2.1.1.min.js%3A2%3A29465%0AI%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fjquery%2Fjquery-2.1.1.min.js%3A2%3A29656%0A%40http%3A%2F%2Flocalhost%3A21425%2Fjs%2Fapp.js%3A15%3A6%0A > angular.min.js:6:415 注入BaseClientProvider提供商。

完整代码在这里plnkr

ClientA