how can i extend a service

后端 未结 6 1338
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

提交回复
热议问题