What is the difference between ['$httpProvider'], $http, $httpPovider etc

1,444 views
Skip to first unread message

Alex Tan

unread,
Jan 21, 2014, 9:53:21 PM1/21/14
to ang...@googlegroups.com
Sometimes I see code like

.config(['$httpProvider', function($httpProvider) {

sometimes it is

angular.module('someModule', [])
.factory('someResource', function ($http, ....) {

so I see few scenarios:
including in square brackets
including $http and other dependencies
including $httpProvider etc witrh the word Provider

Besides this sometimes all these is included in angular.module, sometimes in controllers or services

Can somebody point me to some explanations about all this, it is a bit confusing what, how and when to include

Mauro Servienti

unread,
Jan 22, 2014, 2:47:31 AM1/22/14
to ang...@googlegroups.com
.config(['$httpProvider', function($httpProvider) {

At config time you cannot depend on anything other than providers.
In general the syntax [’name’, function] is to express dependency that survives to magnification.
The AngularJS Dependency Engine, due to the way JavaScript does not work, in the sense that is misses types, can only resolve dependencies by name, thus it inspects the name of the arguments of your function and grab dependencies.
If a magnification process changes the naming AngularJS cannot resolve dependencies anymore, the annotation syntax is there to let your code “survive” to minification.

.m

ThomasBurleson

unread,
Jan 22, 2014, 9:11:29 AM1/22/14
to ang...@googlegroups.com
Nicely said Mauro... with one (1) slight change. I think you meant to say "minification"...
That is what the `uglify` process does: it minifies and obfuscates. 

So the annotation solution [in AngularJS] preserves the original names of the arguments so DI continues to work after the code has been uglified.

Mauro Servienti

unread,
Jan 22, 2014, 9:20:16 AM1/22/14
to ang...@googlegroups.com
Nicely said Mauro... with one (1) slight change. I think you meant to say "minification"...

Yes, I hate the spell checker of my Mac :-D

That is what the `uglify` process does: it minifies and obfuscates. 
So the annotation solution [in AngularJS] preserves the original names of the arguments so DI continues to work after the code has been uglified.

Exactly :-) thanks for pointing my error.

.m
Reply all
Reply to author
Forward
0 new messages