how can i extend a service

后端 未结 6 1293
Happy的楠姐
Happy的楠姐 2020-12-08 07:00

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 metho

相关标签:
6条回答
  • 2020-12-08 07:26

    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

    0 讨论(0)
  • 2020-12-08 07:28

    More cleaner and imperative way

    .factory('ExtendedService', function($http, BasicService){
    
        var extended = angular.extend(BasicService, {})
        extended.method = function() {
            // ...
        }
        return extended;
    }
    
    0 讨论(0)
  • 2020-12-08 07:36

    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

    0 讨论(0)
  • 2020-12-08 07:40

    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()).

    .factory('ExtendedService', function($http, BasicService){
      BasicService['method_four'] = function(){};
      return BasicService;
    }
    
    0 讨论(0)
  • 2020-12-08 07:42

    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.

    0 讨论(0)
  • 2020-12-08 07:46

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

    .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.

    0 讨论(0)
提交回复
热议问题