<rant>
Too many different terms: provider, service, factory sharing more or less the same functionality. They should use less common words with inside meaning. It's confusing until you learn all of them.
And now, Polymer looks pretty similar to Angular, and I kind of like the focus on components. The idea of packaging snippets of HTML, JS and CSS with syntactic sugar is great. Why mix CSS from lots of things into one big file? It's hard to follow the links after a while. Same with JS. Why put so much JS in one place, instead of splitting it into components? Polymer does this divide-and-conquer strategy better.
Not that you can't make components with Angular, but you can't dynamically load them with controller and everything, or at lease you can't do that easily.
</rant>
The benefit of the different types of providers comes in how and when they are used.
Constants are especially interesting because they can be injected into the .config function of a module. No other provider can be injected for the very good reason that .config cannot inject transitive dependencies. Because constants cannot have any dependencies (unlike services and factories) and do not change (unlike values) they are safe to inject during the config phase. FYI, the .run function is invoked (and its dependencies injected of course) after all modules have finished loading and all config functions have been run. If your provider needs another provider as part of its init then you can set that up in the .run function.
Remember that DI in angular is not magic. It is quite literally the creation of a closure whereby angular invokes your provider function with your dependencies passed as arguments.
The difference between factories and services has a lot to do with how they will get used and singularity of them.
I think the mistake of the documentation is that it leads the reader down the path of the convenience functions first rather than instilling a deeper understanding of the core DI mechanism.
As always with Angular docs, they are dense and it always pays off to understand what you are reading as deeply as possible. Use the force, read the source.
IIRC, controllers are indeed the only thing you can't load dynamically. The controller store is strictly private, and controllers can only be registered a initialization time.
Nothing prevents them from making it public, though. I don't know why it is not accessible.
Boy, this topic is one of the most confusing issues for anyone who is trying Angular right now. The Yeoman build system, for example, creates factories by default. The documentation for Angular, or at least when I was going through it last month, hadn't seemed to catch up...so it was hard to know if factory was a best practice or simply an issue of "you say tomatoe, I say tomato"
[+] [-] visarga|12 years ago|reply
And now, Polymer looks pretty similar to Angular, and I kind of like the focus on components. The idea of packaging snippets of HTML, JS and CSS with syntactic sugar is great. Why mix CSS from lots of things into one big file? It's hard to follow the links after a while. Same with JS. Why put so much JS in one place, instead of splitting it into components? Polymer does this divide-and-conquer strategy better.
Not that you can't make components with Angular, but you can't dynamically load them with controller and everything, or at lease you can't do that easily. </rant>
[+] [-] marknutter|12 years ago|reply
[+] [-] michaelw|12 years ago|reply
Constants are especially interesting because they can be injected into the .config function of a module. No other provider can be injected for the very good reason that .config cannot inject transitive dependencies. Because constants cannot have any dependencies (unlike services and factories) and do not change (unlike values) they are safe to inject during the config phase. FYI, the .run function is invoked (and its dependencies injected of course) after all modules have finished loading and all config functions have been run. If your provider needs another provider as part of its init then you can set that up in the .run function.
Remember that DI in angular is not magic. It is quite literally the creation of a closure whereby angular invokes your provider function with your dependencies passed as arguments.
The difference between factories and services has a lot to do with how they will get used and singularity of them.
I think the mistake of the documentation is that it leads the reader down the path of the convenience functions first rather than instilling a deeper understanding of the core DI mechanism.
As always with Angular docs, they are dense and it always pays off to understand what you are reading as deeply as possible. Use the force, read the source.
[+] [-] pygy_|12 years ago|reply
Nothing prevents them from making it public, though. I don't know why it is not accessible.
[+] [-] danso|12 years ago|reply
[+] [-] taude|12 years ago|reply
[+] [-] artjumble|12 years ago|reply
One question though, is there any additional overhead to a service vs factory?
[+] [-] grinich|12 years ago|reply