0

Is there anyway to call another service form current service ?

/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/

Jishad
  • 2,876
  • 8
  • 33
  • 62

2 Answers2

0

yes it's totally possible:

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

Just inject your service and use it.

Fernando Del Olmo
  • 1,440
  • 17
  • 32
  • See, I want to call generatePaginationData function from both getRepairCategory and deleteRepairCategory functions after the http request success. – Jishad Oct 19 '16 at 06:51
  • Sorry, didnt see that. For that i can redirect you to another question like yours http://stackoverflow.com/q/19842669/6691908 but, resuming, can you try using `this`? – Fernando Del Olmo Oct 19 '16 at 06:54
0

Try using the this reference of the service

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;
    }
};
});
Sravan
  • 18,467
  • 3
  • 30
  • 54