Why is an object defined with .constant() available in .config() but not .factory() method?

148 views
Skip to first unread message

Pulkit Singhal

unread,
Apr 3, 2014, 4:23:12 PM4/3/14
to ang...@googlegroups.com
When I setup with a string constant everything works:
https://gist.github.com/pulkitsinghal/9962161#file-works-js

But when I setup a constant which is an object, it works in .config() methods but not inside .factory() methods:

What am I doing wrong?

Lars Christian Jensen

unread,
Apr 3, 2014, 4:43:30 PM4/3/14
to ang...@googlegroups.com
It would be simpler if you provided a jsfiddle or similar. I believe angular will only invoke the factory when something actually depends on it, so are you sure that works.js actually works like you think?

Michael Bielski

unread,
Apr 3, 2014, 4:53:18 PM4/3/14
to ang...@googlegroups.com

Pulkit Singhal

unread,
Apr 3, 2014, 5:27:43 PM4/3/14
to ang...@googlegroups.com
The docs state: "Register a constant service, such as a string, a number, an array, an object or a function..."

They do not cover why it works as an object in a .config() method but not a .factory() method, unless I don't understand something about the 2 liens that are written there? Please elaborate for me.

Pulkit Singhal

unread,
Apr 3, 2014, 5:30:03 PM4/3/14
to ang...@googlegroups.com
I'll try to setup a jsfiddle and get back, in the mean time, yes I do have code that depends on factory and therefore the console.log statement can be seen in chrome at runtime ... and when its spits out undefined for the object in factory, I wonder if its an injection bug or something I don't know how to configure. Again, I'll get back with a jsfiddle or the like to help demo it.

Michael Bielski

unread,
Apr 3, 2014, 5:32:22 PM4/3/14
to ang...@googlegroups.com
Sorry, but you'll have to reach out to the Angular team for the "Why" part.

Pulkit Singhal

unread,
Apr 3, 2014, 6:19:47 PM4/3/14
to ang...@googlegroups.com
Wouldn't you know it, when I tried setting up the fiddle, it all just worked :)
http://jsfiddle.net/pulkitsinghal/xsDdY/

Thanks for the suggestion @Lars.

Sander Elias

unread,
Apr 4, 2014, 12:50:06 AM4/4/14
to ang...@googlegroups.com
Hi Pulkit,

Because it just works perhaps? So there is no need to explain something that isn't happening? ;)
If you can provide a working not working example, you have found a bug, and should file a bug at github.
When you are not sure it's a bug, and want your code reviewed before filing a bug, post
the working not working example here as a fiddle or plunk, and there are probably a couple of 
people that will review it for you.

Regards
Sander


Pulkit Singhal

unread,
Apr 4, 2014, 12:52:04 AM4/4/14
to ang...@googlegroups.com
Hey Sander! Thanks for responding.

As you may not have seen my previous post 6 hours ago:
Wouldn't you know it, when I tried setting up the fiddle, it all just worked :)
http://jsfiddle.net/pulkitsinghal/xsDdY/
Thanks for the suggestion @Lars.

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/-2Kv_2jRCoo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Sander Elias

unread,
Apr 4, 2014, 1:02:57 AM4/4/14
to ang...@googlegroups.com
Hi Pullkit,

I had the time-line wrong ;)

Regards
Sander
Reply all
Reply to author
Forward
0 new messages