Question: Reuse Services without circular dependencies


Reuse Services without circular dependencies

Answers 1
Added at 2017-09-06 18:09

I have one service that is globally storing all my data used through out my app.

GlobalDataService (GDS)

        .service('GlobalDataService', GlobalDataService);
    GlobalDataService.$inject = ['$http', 'LineStatusService'];
    function GlobalDataService($http, LineStatusService) {
        var gds = this; = {
            //all my data
        } = LineStatusService.getLineStatus().then...

And a simple crud service that handles the Status of my data.

StatusDataService (SDS)

        .service('LineStatusService', LineStatusService);
    LineStatusService.$inject = ['$http', 'GlobalDataService'];
    function LineStatusService($http, GlobalDataService) {
        var service = {
            getLineStatus: getLineStatus,
            saveLineStatus: saveLineStatus,

        function saveLineStatus (line, status, user) {
            var data = {
                status: {
                    status_id: status.status_id,
                    status_desc: status.status_desc
                updated_by: user

            return $'/api/euauto/v1/delivery-status/linestatus', data)
            .then(function successCallback(response) {
      [id].status = status;
            }).catch(function errorCallback(response) {


        return service;

The GDS has to request all Status's when the app first loads, then the Status Service handles any other data requests.

Now I understand you can't have circular dependencies, therefore my plan was to have my Controller handle the save and update using SDS and ALSO update the GDS.

Potential Solution

        .controller('MyController', MyController);

    MyController.$inject = ['GlobalDataService', 'LineStatusService'];
    function MyController(GlobalDataService, LineStatusService) {
        function changeStatus(line, status, user) {
            //do a thing
            //and another
            LineStatusService.saveLineStatus(line, status, user);
  [id] = status;

The Problem

My question is, now I want to develop a new Controller which contains the exact same functionality I will now have to remember to copy the same code and business logic from my original Controller to reuse both Services. Also, if the GDS doesn't depend on SDS it won't be able to getLineStatus() on load and each Controller in the app will have to remember to getLineStatus() on load.

Ideally all the logic and requests should be contained in one place, preferably my SDS. My GDS data should be consistent across the whole Application.

Answers to

Reuse Services without circular dependencies

nr: #1 dodano: 2017-09-06 18:09

If GlobalDataService is supposed to be initialized with data when the app starts, then you could initialize it in a .run() block instead of in the service constructor. That way the GDS does not need to have the other services injected at all. The other data services can then have GDS injected without circular dependency issues.

    .run(function(GlobalDataService, LineStatusService) { = LineStatusService.getLineStatus().then...

Source Show
◀ Wstecz