I sometimes wish I had a short and easy to understand explanation of when to use each service type in AngularJS. Here it is: A controller is not one of the 5 services, but it is something chained off the angular.module(..') in your Javascript files. The key thing to remember about controllers is that they are tied to specific segments of HTML. This should be as thin as possible, delegating as much work as they can to the 5 different services. This service actually uses the "service" keyword in the code, and is the go-to for building helper classes, async handlers, and all-around singletons. Inside of a service you define properties and methods on this, and then you can just call them as public methods on the name of the service object that you inject into your controllers. You use the factory when you need objects. When creating a factory, you define an object and return it. It's especially useful when you have an array full of object.s Providers are used when you want to make services and factories that are configurable. This is done with a config method. I rarely ever use these. The constants are for, well, when you want to use constants defined for your whole module! This could be a string, an array, anything really. The point is that it's an object that can't be set later. This, like the constant service, is a ingle value or object. While in the constant service uses the constant keyword and is unable to be changed, the value service uses the value keyword and can be changed later.
0 Comments
Your comment will be posted after it is approved.
Leave a Reply. |
AuthorThe posts on this site are written and maintained by Jim Lynch. About Jim...
Categories
All
Archives
March 2023
|