Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 创建角度基本服务和子服务_Angularjs_Prototypal Inheritance_Angular Services - Fatal编程技术网

Angularjs 创建角度基本服务和子服务

Angularjs 创建角度基本服务和子服务,angularjs,prototypal-inheritance,angular-services,Angularjs,Prototypal Inheritance,Angular Services,我正在尝试为动态列表对象创建一个通用服务,对于不同类型的对象,我需要稍微不同的方法来实现这个服务。所以我认为最好有一个基本服务和一些子服务。问题是,我需要根据子服务使用不同的对象初始化基本服务 到目前为止,我得到的是: 基本列表服务(简称为相关) 列表服务的子服务 App.factory('UserList', [ 'User', 'List','$q', function (User, List) { UserList = funct

我正在尝试为动态列表对象创建一个通用服务,对于不同类型的对象,我需要稍微不同的方法来实现这个服务。所以我认为最好有一个基本服务和一些子服务。问题是,我需要根据子服务使用不同的对象初始化基本服务

到目前为止,我得到的是:

基本列表服务(简称为相关)

列表服务的子服务

App.factory('UserList', [
        'User', 'List','$q',
        function (User, List) {


            UserList = function(){

                var searchParams = {
                    // params Object
                };
                return new List(User, searchParams);
            };
            // extend base class:
            UserList.prototype.updateUser = function(id){
                  //.....
            }
            //....
            return UserList;
}]);
目前只加载了UserList,但是:当然,每次调用新实例时都会加载,这是由于调用
new
操作符,但我只需要一个实例。但是离开
new
操作符抛出的错误是
this.nextPage()将是未定义的函数。除此之外,似乎没有应用扩展函数
updateUser

那么,在angular中将参数传递给父服务时,从其他服务继承的最佳实践是什么呢?

我必须这样做

已将子服务更改为此,以从基继承正确的子服务:

App.factory('UserList', [
    'User', 'List','$q',
    function (User, List) {

        var UserList = function(){
            var searchParams = {
                //.....
            };
            List.call(this, User, searchParams);
        };

        // inherit from List service
        UserList.prototype = Object.create(List.prototype);
        UserList.prototype.updateUser = function(id) {
            //.....
        };

        return UserList;
    }
])
)

有用资源
App.factory('UserList', [
    'User', 'List','$q',
    function (User, List) {

        var UserList = function(){
            var searchParams = {
                //.....
            };
            List.call(this, User, searchParams);
        };

        // inherit from List service
        UserList.prototype = Object.create(List.prototype);
        UserList.prototype.updateUser = function(id) {
            //.....
        };

        return UserList;
    }
])