0

I've stumbled upon this question here by looking for the reason, that a well defined factory is not loaded. Then I wondered about the terms "defined in an angular module".

If I have kind of a service like this:

angular.module('d3', ['underscore'])
       .factory('d3', ['$window', 'underscore',
       function($window, _) {
           if (!_.isObject($window.d3)) {
               throw "services.d3: moment library missing!";
           } else {
               return $window.d3;
          }
       }
   ]);

And when I try to load this service in a module like this:

angular.module("widgets.D3Widget", ['underscore'])

.controller("widgets.D3Widget.D3WidgetCtrl", [
    "$scope",
    "d3",
    function(
        $scope,
        d3
    ) {
        console.log(d3.select('body'));
        //work goes here
    }
]);

the service is not loaded. I rather get an Angular error like: "Error: [$injector:unpr] Unknown provider: d3Provider <- d3.

I've read the Angular docu about DI, but that didn't help very much.

So what is meant by 'defining the service'? Writing the module d3 seems not to be enough, I need to add it in a second module, like angular.module("widgets.D3Widget", ['underscore', 'd3']), not just in a component of a module (controller, directive, whatever). Is that right?

1 Answer 1

0

Sorry, cannot post comments. You need to add the 'd3' dependency when configuring module widgets.D3Widget, like this

angular.module("widgets.D3Widget", ['d3'])

This is because 'd3' is not residing under the same module as your 'widgets.D3Widget'.

Sign up to request clarification or add additional context in comments.

1 Comment

Alright, so if I have a component of a module and I want to use it in another module, I need to add it in at least one other module, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.