0

如何在角度服务中定义函数,然后在另一个服务的函数中使用函数定义。我实现如下,但它不起作用。我不想返回 ParentService 实例,只是将其构造函数的定义用作 PersonService 的父类。

谢谢你的帮助。

var personModule = angular.module('personModule',['coreModule']).service('personService', function($q, jAjax){

    function PersonService($q, jAjax) {
        var self = this;
        ParentService.call(self, $q, jAjax);
        this.name = "person";
    };

    inherit(ParentService, PersonService);

  PersonService.prototype.loadPersonList = function() {
        var self = this;
        var deferred = self.$q.defer();
        ParentService.prototype.loadList.call(self, "meta", function(data){
            self.items = data;
            deferred.resolve(data);
        });
        return  deferred.promise;
    };

  return new PersonService($q);
});

var coreModule = angular.module('coreModule',[]).service('commonService', function(jAjax){

 var ParentService = function(jAjax) {
 };

 ParentService.prototype.loadList = function(docType, fn) {
    var self = this;
    var url = self.name + "/get";

    self.jAjax.get(url).success(function(data) {
        if (fn !== undefined) {
            fn.call(this, data);
        }
    });
 };

 return ParentService;
});
4

1 回答 1

0

谢谢你的时间。我找到了解决方案。在 parentService 中,我返回一个包含函数定义的对象

return {ParentService:ParentService};

,然后在其他模块中我将其作为属性

inherit(commonService.ParentService, PersonService);

固定代码如下

var personModule = angular.module('personModule',['coreModule']).service('personService', function($q, jAjax, commonService){

function PersonService($q, jAjax, commonService) {
    var self = this;
    ParentService.call(self, $q, jAjax);
    this.name = "person";
};

inherit(commonService.ParentService, PersonService);

PersonService.prototype.loadPersonList = function() {
    var self = this;
    var deferred = self.$q.defer();
    commonService.ParentService.prototype.loadList.call(self, "meta", function(data){
        self.items = data;
        deferred.resolve(data);
    });
    return  deferred.promise;
};

return new PersonService($q, jAjax, commonService);
 });

  //Calling module
 var coreModule = angular.module('coreModule',[]).service('commonService',function(jAjax){

 var ParentService = function(jAjax) {
 };

 ParentService.prototype.loadList = function(docType, fn) {
  var self = this;
  var url = self.name + "/get";

  self.jAjax.get(url).success(function(data) {
     if (fn !== undefined) {
         fn.call(this, data);
     }
    });
  };

  return {ParentService:ParentService};
  });
于 2013-08-09T07:35:03.687 回答