kumuda
kumuda

Reputation: 509

Call to a function within $get of Provider modules causes TypeError

This is my provider.

App.provider('cloudinaryDetails', function(CloudinaryProvider){

function setCloudinaryDetails(cloudinaryDetails){
    CloudinaryProvider.configure({
        cloud_name: cloudinaryDetails.cloud_name,
        api_key: cloudinaryDetails.api_key
    });
}

this.$get = function($http){
    return {
            initialize: function(){
            return $http.get('path/to/api').then(function(response){
                setCloudinaryDetails(response.data);
            });
        }
    };
};

});

I am calling the initialize function in config module

App.config(function(cloudinaryDetailsProvider){
cloudinaryDetailsProvider.initialize();

});

Console Error:

[$injector:modulerr] Failed to instantiate module App due to: TypeError: cloudinaryDetailsProvider.initialize is not a function

Upvotes: 1

Views: 604

Answers (1)

Joel Kornbluh
Joel Kornbluh

Reputation: 1437

Cause:

You are injecting the cloudinaryDetailsProvider "provider" but initialize is not defined on the provider, it is defined on the instance.

Suggested Fix:

To get a reference to the cloudinaryDetails "instance", create a run block and inject the instance instead of the provider.

App.run(function(cloudinaryDetails){
  cloudinaryDetails.initialize();
});

See AngularJS docs for an explanation on how providers work

Upvotes: 0

Related Questions