Possible misunderstanding in Docs

19 views
Skip to first unread message

Raptis Dimos

unread,
Feb 27, 2015, 10:03:26 AM2/27/15
to ang...@googlegroups.com
Hello, I found in the page https://docs.angularjs.org/tutorial/step_07, of the docs a part saying "Note: Providers can only be injected into config functions. Thus you could not inject $routeProvider into PhoneListCtrl".

However, providers can normally be injected inside controllers, right ?
Have i missed anything or is this statement misleading ?
Thanks a lot!

Dimos

Sander Elias

unread,
Feb 27, 2015, 10:20:43 AM2/27/15
to ang...@googlegroups.com

Hi Raptis,

Its a matter of wording. I’ll try to explain. You can create a provider. The normal use of an provider returns a factory, which you can inject into your code. However, angular allows you to create providers that may have configuration options. If you need to configure this options, you can inject the provider itself into the config functions.
as an example angular.module(test,[]).provider('sample', fucntion (){..}) returns actually 2 things.(hmmm, well for the sake of this argument it does!)
first it returns a factory named sample, but also it returns a provider called sampleProvider, the last one, you can only use inside the aforementioned config functions, and you can't use it anywhere else.

Does that clear it up enough for you?
Regards
Sander

Raptis Dimos

unread,
Feb 27, 2015, 10:32:18 AM2/27/15
to ang...@googlegroups.com
Oh,

Yes, I think now I am in a better state :P.
I had fully understood that providers return 2 things (unlike factories and services), even though I had understood that they only can be injected in config().
So, factories, services and providers (in fact, the factories returned by providers) can all be injected in controllers.
However, only providers (which are like the ''wrappers'' of factories and services) can be injected in config() function.

Thanx a lot!!
Reply all
Reply to author
Forward
0 new messages