0

I am trying to inject a custom factory from one module into a custom provider from another module. What I really want to do is to use the custom factory inside the config block but I can't, so I'm trying to configure a provider that uses the custom factory which will then get injected into config.

I have been trying to inject my custom factory into the provider but I can't seem to get it right. I don't know if it's syntax or maybe my approach is wrong. My questions are:

1.) is this even possible?
2.) is my syntax correct?

Here is the factory:

.factory('myFactory', myFactory);
  myFactory.$inject = ['$q', '$http', 'Story'];
  function myFactory($q, $http) {

    return {
        getSomething: getSomething,
    }

    function getSomething() {

    }
}

Here is the provider:

.provider('myProvider', function() {
    return {
        $get: function(myFactory) {
            function getStuff() {
                return myFactory.getSomething().then(function(data){
                    return data;
                })
            }
            return {
                stuff: getStuff
            }
        }
    }
})

The error that I am getting is this:

Cannot read property 'getSomething' of undefined

Is this the correct use of a provider? I feel like I may be missing something. Thanks!

user3667725
  • 111
  • 1
  • 9

1 Answers1

0

During the configuration phase, you can't access services:

During application bootstrap, before Angular goes off creating all services, it configures and instantiates all providers. We call this the configuration phase of the application life-cycle. During this phase, services aren't accessible because they haven't been created yet.

https://docs.angularjs.org/guide/providers

Brad Barber
  • 2,953
  • 1
  • 19
  • 18