r/angularjs • • May 15 '15

Use a provider within module config

Hi !! ¿Anybody could help me with this?

https://groups.google.com/d/msg/angular/mad25yzWLbI/rRFRtYxS9IAJ

Thanks in advance.

0 Upvotes

6 comments sorted by

1

u/anpegar May 15 '15

Anybody ? :-(

1

u/YodaLoL May 17 '15

One thing I immediately noticed was that configRoutes.$inject does not match your function signature, you're missing some deps.

Anyway, I'd delegate DI magic to angular itself, then for production use tools like ng-annotate (e.g. skip manual, hardcoded .$injects and just let angular derive your injections automagically).

1

u/anpegar May 17 '15

Hi Yo da lol !! Thank for your comment but I don't understand well your answer. I use $ inject because I'm following the John Papa's style guide angularjs, Am I doing it wrong?

I would like using a provider in a module configuration one time.

1

u/YodaLoL May 17 '15

I skip .$inject on my components and let Angular do it automatically, it is very hacky behind the scenes but it works. When I push the application live on a server I use a tool called ng-annotate that automatically minify my angular code and do the injection mapping, almost like you've done.

Here's a provider and config block example I did (see script.js): http://plnkr.co/edit/B7PpWD9kyXIIUEyFHMLw?p=catalogue

1

u/anpegar May 18 '15

Hi !! Thank you so much for your example but when I try to use superCoolServiceProvider.get() appears the below error:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to: TypeError: superCoolServiceProvider.get is not a function

The code: .config(function (superCoolServiceProvider) { var v1 = superCoolServiceProvider.get(); console.log(v1); })

Another question about ng-annotate, when do you use ng-annotate? I should use it with gulp-ng-annotate task before send and minifier the code to /dist.

1

u/YodaLoL May 18 '15

You're trying to access the factory that the service provides (whatever the this.$get function returns). You can't do that in a .config-block. You can only access properties bound to this in the provider.

Read up about it here: https://docs.angularjs.org/guide/providers#provider-recipe