Search code examples
angularjsdependency-injectionangularjs-service

Angular service wire up not working


I have written a service, depending on an other service. But initialisation is not working.

You can find a plunker as showcase

Should be close to working... Any tipps? Thanks in advance!

edit: The plunker is fixed now and can be used as reference.


Solution

  • You need to either change your testServiceMockConfig and testService from factory to service, for example:

    .service('testServiceMockConfig', function ()
    

    or keep them as factories and add return.this; at the bottom of both of them or restructure them like this (recommended):

    angular.module('testServiceMockConfig', [])
      .factory('testServiceMockConfig', function() {
        console.log("setup cqrs mock config.");
    
        return {
          doLoadItems: function(callback) {
            console.log("mock loading data");
            if (!this.configuredLoadItems) {
              throw Error("The mock is not configured to loadItems().");
            }
            callback(this.loadItemsError, this.loadItemsSuccess);
          },
          whenLoadItems: function(success, error) {
            this.configuredLoadItems = true;
            this.loadItemsSuccess = success;
            this.loadItemsError = error;
          }
        };
      });
    

    I also assume that loadItems in testService should call:

    testServiceMockConfig.doLoadItems(callback);
    

    instead of:

    testService.doLoadItems(callback);