从服务功能调用另一个服务功能

时间:2016-10-19 06:39:41

标签: angularjs angular-services

无论如何都要拨打当前服务的其他服务吗?

/ FACTORIES /

app.service('p3call',function($http,$rootScope){
return {
    getRepairCategory:function(url){
        $http.post(url)
        .success(function (response){
            generatePaginationData(response);
        });
    },
    deleteRepairCategory:function(request,url){
        $http.post(url,request)
        .success(function (response){
            generatePaginationData(response)
        });
    },
    generatePaginationData:function(response){
        var pages = [];
        $rootScope.categories = response.data;
        $rootScope.currentPage = response.current_page;
        $rootScope.totalPages = response.last_page;
        for(var i=1;i<=response.last_page;i++) {          
            pages.push(i);
        }
        $rootScope.range = pages;
    }
};
});

/ FACTORIES /

2 个答案:

答案 0 :(得分:0)

是的,它完全有可能:

app.service('p3call',function($http,$rootScope, myOtherService){
    return {
        getRepairCategory:function(url){
            ...
        },
        deleteRepairCategory:function(request,url){
            ...
        },
        generatePaginationData:function(response){
            ...
        }
        myFunction(){
            myOtherService.newFunction();
        }
    };
});

只需注入您的服务并使用它。

答案 1 :(得分:0)

尝试使用服务的this引用

app.service('p3call',function($http,$rootScope){
var this = this;
return {
    getRepairCategory:function(url){
        $http.post(url)
        .success(function (response){
            this.generatePaginationData(response);
        });
    },
    deleteRepairCategory:function(request,url){
        $http.post(url,request)
        .success(function (response){
        this.generatePaginationData(response)
        });
    },
    generatePaginationData:function(response){
        var pages = [];
        $rootScope.categories = response.data;
        $rootScope.currentPage = response.current_page;
        $rootScope.totalPages = response.last_page;
        for(var i=1;i<=response.last_page;i++) {          
            pages.push(i);
        }
        $rootScope.range = pages;
    }
};
});