Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Inject provider to module.config

What I'm doing wrong?
According to documentation, I should be able to inject the provider to module.config...but I'm getting an error - "Unknown Provider"

http://jsfiddle.net/g26n3/

(function () {
    "use strict";

    angular.module("ab.core", [])
        .provider("ab.core.provider", function () {
            console.log("ab.core.provider - constructor");
            this.$get = function () {
                console.log("ab.core.provider - get");
                return { value: "test" };
            }
        })
        .config(["ab.core.provider", function (myProvider) { console.log("ab.core - config " + myProvider.value); }])
        .run(function () { console.log("ab.core - run"); });

    angular.module("ab", ["ab.core"])
        .config(["ab.core.provider", function () { console.log("ab - config"); }])
        .run(function () { console.log("ab - run"); });

    angular.bootstrap(document, ['ab']);

}());

Actually I have three questions here...
1) How to inject the ab.core.provider to config of ab.core module.
2) How to inject the same provider (ab.core.provider) to config of ab module.
3) If I will inject the same provider to config of both modules, it will be the same instance of provider or it will be two different instances?

Thank you!

like image 587
Alex Dn Avatar asked Nov 13 '13 22:11

Alex Dn


People also ask

How do I inject a module in AngularJS?

Injecting a value into an AngularJS controller function is done simply by adding a parameter with the same name as the value (the first parameter passed to the value() function when the value is defined). Here is an example: var myModule = angular. module("myModule", []); myModule.

What is $inject in AngularJS?

Dependency Injection in AngularJS can be defines as the software design pattern which defines the way the software components are dependent on each other. AngularJS provides a set of components that can be injected in the form of dependencies such as factory, value, constant, service, and provider.

Which components Cannot be injected as a dependency in AngularJS?

Note that you cannot inject "providers" into run blocks. The config method accepts a function, which can be injected with "providers" and "constants" as dependencies. Note that you cannot inject "services" or "values" into configuration.

When you inject a service into a controller?

When a service or controller needs a value injected from the factory, it creates the value on demand. It normally uses a factory function to calculate and return the value. Let's take an example that defines a factory on a module, and a controller which gets the factory created value injected: var myModule = angular.


1 Answers

You need to add the "Provider" suffix, that's how Angular knows, but like shaunhusain said in the comments there are some limitations:

http://jsfiddle.net/g26n3/1/

angular.module("ab.core", [])
  .provider("ab.core.provider", function () {

  })
  .config(["ab.core.providerProvider", function(p) {
    ...  
  }]

angular.module("ab", ["ab.core"])
  .config(["ab.core.providerProvider", function(p) {
    ...
  }]

Follow naming conventions so it looks good, .provider('camelCase', ...)

like image 59
elclanrs Avatar answered Sep 22 '22 20:09

elclanrs