2013-03-08 90 views
30

我对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; 
    } 
); 

现在我想定义从上面BasicService延伸,使得我可以使用从我的扩展服务的BasicService下定义的方法扩展服务。也许是这样的:

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

回答

21

ExtendedService应注入BasicService为了能够访问它。除此之外,BasicService是一个对象字面量,因此您不能将其实际称为函数(BasicService())。

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

完美的感谢,绝对是我一直在寻找。 – Amyth 2013-03-19 13:55:08

+0

如果我们认为BasicService是抽象的,我是否认为这是最好的方法?因为那样我们就永远不会真的使用BasicService,只有扩展服务,所以文学扩展它没有问题。 – Floris 2015-07-14 14:04:37

+0

这个方法真的好吗?鉴于服务是单身人士,这意味着BasicService上的更改将是永久性的,所以BasicService也将被更改。创建BasicService的副本并返回该副本的扩展版本不是更好吗? – Razvan 2016-02-05 11:21:28

3

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

要当心,因为是单身 这样你就可以一次延伸服务/工厂,延伸服务/工厂。

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

,但如果你不喜欢

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

它的工作原理

+1

很好的说明,但angular.extend()通过制作服务的副本来以更简单的方式照顾它。 – Alkaline 2014-08-28 11:30:14

68

更清洁和必要的方式

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

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

我认为最好扩展空对象而不是原始服务:'var extended = angular.extend({},BasicService)''。'copy'将创建新的实例,而不是使用它的浅拷贝来扩展 – havenchyk 2014-11-24 08:04:44

+0

完美!非常干净... – 2015-01-28 17:29:50

16

在我看来,一个更好的办法:

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

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

    return service; 
}); 

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

+0

我同意,使用copy()会更有意义,因为您不会更改继承的服务。 – 2016-08-31 20:58:03

-1

为了扩展的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(){ 

    } 
} 

这有原来的服务是不会改变的好处,同时保持它的功能。您也可以选择在ExtendedService中为BasicService中的其他3种方法使用自己的实现。

+0

不是海报,但有人可以解释为什么这个解决方案downvoted?我看起来很好。 – Floris 2015-07-14 14:00:00

+0

看来,这篇文章是downvoted,因为你需要列出你从父对象得到的方法和字段。由于某些原因,这是不好的做法。从技术上讲,这不是纯粹的继承。 – 2015-07-22 11:06:02