如何扩展服务

时间:2022-08-22 21:17:28

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.

对于angularjs来说,我是一个新手,我找不到任何相关的文档或例子。我要做的是扩展一个基本的服务,这样我就可以使用其他服务在基本服务下定义的方法。比如我有一个基本的服务,如下所示。

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:

现在,我想定义一个扩展的服务,它从上面的BasicService扩展,这样我就可以使用从我的扩展服务中定义的BasicService定义的方法。也许类似:

    factory('ExtendedService', function($http){
        var ExtendedService = BasicService();
        ExtendedService['method_four'] = function(){/* code for method four */}
        return ExtendedService;
    }

6 个解决方案

#1


21  

Your ExtendedServiceshould inject the BasicServicein order to be able to access it. Beside that BasicService is an object literal, so you can't actually call it as function (BasicService()).

您的ExtendedServiceshould注入BasicServicein以便能够访问它。在BasicService的旁边是一个对象文字,因此您不能实际地将它调用为function (BasicService())。

.factory('ExtendedService', function($http, BasicService){
  BasicService['method_four'] = function(){};
  return BasicService;
}

#2


68  

More cleaner and imperative way

更干净、更迫切的方式

.factory('ExtendedService', function($http, BasicService){

    var extended = angular.extend(BasicService, {})
    extended.method = function() {
        // ...
    }
    return extended;
}

#3


16  

In my opinion, a better way:

在我看来,更好的办法是:

.factory('ExtendedService', function($http, BasicService){
    var service = angular.copy(BasicService);

    service.methodFour = function(){
        //code for method four
    };

    return service;
});

Here at least does not change the inherited service.

这里至少不会更改继承的服务。

#4


3  

Sorry if I post here but may be it's a good place to do it. I refer to this post

对不起,如果我在这里张贴,但可能是一个好地方。我指的是这篇文章

watch out to extend a service/factory because are singleton so you can extend a service/factory once.

注意扩展服务/工厂,因为是单例的,所以可以只扩展一次服务/工厂。

'use strict';
            angular.module('animal', [])
                .factory('Animal',function(){
                        return {
                            vocalization:'',
                            vocalize : function () {
                                console.log('vocalize: ' + this.vocalization);
                            }
                        }

                });
                angular.module('dog', ['animal'])
                    .factory('Dog', function (Animal) {
                        Animal.vocalization = 'bark bark!';
                        Animal.color = 'red';
                        return Animal;
                    });

                angular.module('cat', ['animal'])
                   .factory('Cat', function (Animal) {
                        Animal.vocalization = 'meowwww';
                        Animal.color = 'white';
                        return Animal;
                    });
                 angular.module('app', ['dog','cat'])
                .controller('MainCtrl',function($scope,Cat,Dog){
                     $scope.cat = Cat;
                     $scope.dog = Dog;
                     console.log($scope.cat);
                     console.log($scope.dog);
                    //$scope.cat = Cat;
                });

but if you do like

但如果你喜欢的话

'use strict';
            angular.module('animal', [])
                .factory('Animal',function(){
                    return function(vocalization){
                        return {
                            vocalization:vocalization,
                            vocalize : function () {
                                console.log('vocalize: ' + this.vocalization);
                            }
                        }
                    }
                });    
                angular.module('app', ['animal'])
                    .factory('Dog', function (Animal) {
                        function ngDog(){
                            this.prop = 'my prop 1';
                            this.myMethod = function(){
                                console.log('test 1');
                            }
                        }
                        return angular.extend(Animal('bark bark!'), new ngDog());
                    })
                    .factory('Cat', function (Animal) {
                        function ngCat(){
                            this.prop = 'my prop 2';
                            this.myMethod = function(){
                                console.log('test 2');
                            }
                        }
                        return angular.extend(Animal('meooow'), new ngCat());
                    })
                .controller('MainCtrl',function($scope,Cat,Dog){
                     $scope.cat = Cat;
                     $scope.dog = Dog;
                     console.log($scope.cat);
                     console.log($scope.dog);
                    //$scope.cat = Cat;
                });

it works

它的工作原理

#5


1  

I wrote an $extend provider that uses Backbone's extend under the hood -- So you get both prototype and static property extending in case you need them -- Plus you get parent/child constructors -- see gist @ https://gist.github.com/asafebgi/5fabc708356ea4271f51

我编写了一个$extend提供程序,它使用了主干的扩展——如果需要的话,您可以获得原型和静态属性扩展——以及父/子构造函数——请参见gist @ https://gist.github.com/asafebgi/5fabc708356ea4271f51

#6


-1  

To expand on Stewie's answer, you could also do the following:

要扩展Stewie的答案,你也可以做以下事情:

.factory('ExtendedService', function($http, BasicService){
    var service = {
        method_one: BasicService.method_one,
        method_two: BasicService.method_two,
        method_three: BasicService.method_three,
        method_four: method_four
    });

    return service ;

    function method_four(){

    }
}

This has the benefit that the original service is not altered, while keeping it's functionality. You can also choose to use your own implementations in the ExtendedService for the other 3 methods from BasicService.

这样做的好处是,原始服务不会被更改,同时保留它的功能。您还可以为BasicService的其他3个方法在ExtendedService中使用自己的实现。

#1


21  

Your ExtendedServiceshould inject the BasicServicein order to be able to access it. Beside that BasicService is an object literal, so you can't actually call it as function (BasicService()).

您的ExtendedServiceshould注入BasicServicein以便能够访问它。在BasicService的旁边是一个对象文字,因此您不能实际地将它调用为function (BasicService())。

.factory('ExtendedService', function($http, BasicService){
  BasicService['method_four'] = function(){};
  return BasicService;
}

#2


68  

More cleaner and imperative way

更干净、更迫切的方式

.factory('ExtendedService', function($http, BasicService){

    var extended = angular.extend(BasicService, {})
    extended.method = function() {
        // ...
    }
    return extended;
}

#3


16  

In my opinion, a better way:

在我看来,更好的办法是:

.factory('ExtendedService', function($http, BasicService){
    var service = angular.copy(BasicService);

    service.methodFour = function(){
        //code for method four
    };

    return service;
});

Here at least does not change the inherited service.

这里至少不会更改继承的服务。

#4


3  

Sorry if I post here but may be it's a good place to do it. I refer to this post

对不起,如果我在这里张贴,但可能是一个好地方。我指的是这篇文章

watch out to extend a service/factory because are singleton so you can extend a service/factory once.

注意扩展服务/工厂,因为是单例的,所以可以只扩展一次服务/工厂。

'use strict';
            angular.module('animal', [])
                .factory('Animal',function(){
                        return {
                            vocalization:'',
                            vocalize : function () {
                                console.log('vocalize: ' + this.vocalization);
                            }
                        }

                });
                angular.module('dog', ['animal'])
                    .factory('Dog', function (Animal) {
                        Animal.vocalization = 'bark bark!';
                        Animal.color = 'red';
                        return Animal;
                    });

                angular.module('cat', ['animal'])
                   .factory('Cat', function (Animal) {
                        Animal.vocalization = 'meowwww';
                        Animal.color = 'white';
                        return Animal;
                    });
                 angular.module('app', ['dog','cat'])
                .controller('MainCtrl',function($scope,Cat,Dog){
                     $scope.cat = Cat;
                     $scope.dog = Dog;
                     console.log($scope.cat);
                     console.log($scope.dog);
                    //$scope.cat = Cat;
                });

but if you do like

但如果你喜欢的话

'use strict';
            angular.module('animal', [])
                .factory('Animal',function(){
                    return function(vocalization){
                        return {
                            vocalization:vocalization,
                            vocalize : function () {
                                console.log('vocalize: ' + this.vocalization);
                            }
                        }
                    }
                });    
                angular.module('app', ['animal'])
                    .factory('Dog', function (Animal) {
                        function ngDog(){
                            this.prop = 'my prop 1';
                            this.myMethod = function(){
                                console.log('test 1');
                            }
                        }
                        return angular.extend(Animal('bark bark!'), new ngDog());
                    })
                    .factory('Cat', function (Animal) {
                        function ngCat(){
                            this.prop = 'my prop 2';
                            this.myMethod = function(){
                                console.log('test 2');
                            }
                        }
                        return angular.extend(Animal('meooow'), new ngCat());
                    })
                .controller('MainCtrl',function($scope,Cat,Dog){
                     $scope.cat = Cat;
                     $scope.dog = Dog;
                     console.log($scope.cat);
                     console.log($scope.dog);
                    //$scope.cat = Cat;
                });

it works

它的工作原理

#5


1  

I wrote an $extend provider that uses Backbone's extend under the hood -- So you get both prototype and static property extending in case you need them -- Plus you get parent/child constructors -- see gist @ https://gist.github.com/asafebgi/5fabc708356ea4271f51

我编写了一个$extend提供程序,它使用了主干的扩展——如果需要的话,您可以获得原型和静态属性扩展——以及父/子构造函数——请参见gist @ https://gist.github.com/asafebgi/5fabc708356ea4271f51

#6


-1  

To expand on Stewie's answer, you could also do the following:

要扩展Stewie的答案,你也可以做以下事情:

.factory('ExtendedService', function($http, BasicService){
    var service = {
        method_one: BasicService.method_one,
        method_two: BasicService.method_two,
        method_three: BasicService.method_three,
        method_four: method_four
    });

    return service ;

    function method_four(){

    }
}

This has the benefit that the original service is not altered, while keeping it's functionality. You can also choose to use your own implementations in the ExtendedService for the other 3 methods from BasicService.

这样做的好处是,原始服务不会被更改,同时保留它的功能。您还可以为BasicService的其他3个方法在ExtendedService中使用自己的实现。