I need a clear understanding of service class in angular.Both service class and typescript class are the same use case in my view then what is the difference between two?
-
1Start with the angular documentation (see also the 1st example on that page). What do you not understand after reading it? Is there a specific question you still have about what a service is as angular defines it?Igor– Igor2019-06-11 11:03:29 +00:00Commented Jun 11, 2019 at 11:03
-
service class: We do logical coding in service class and import service in component class using constructor and use the service class instance , Typescript Class: we do logical coding in typescript class and import in component class and use the typescript class instance. Then what is the difference between these two.It seems both are same Then why angular use service as special.Vijay VJ– Vijay VJ2019-06-11 11:11:00 +00:00Commented Jun 11, 2019 at 11:11
-
1From Angular perspective it is the same thing. You can think of a service as a regular class that is provided and used with a DI (Dependency Injection). But it is still just a basic class.miselking– miselking2019-06-11 11:19:27 +00:00Commented Jun 11, 2019 at 11:19
-
Please define what you mean by Typescript class? This doesn't make sense to me, it's as if you're placing some kind of magical emphasis on it being a class written in Typescript.David Barker– David Barker2019-06-11 11:55:35 +00:00Commented Jun 11, 2019 at 11:55
Add a comment
|
1 Answer
In case of implementation all angular service , component , module , pipes .. are just typescript class the only difference between each of them is the decorator,but it 'is possible for any service to don't have a decorator if it's has no dependency
finally to declare any class as service you need add this class to any provider list for module or component so you can injected.
so just look for class decorator and if the class added to any provider list
3 Comments
Vijay VJ
Service class: We do logical coding in service class and import service in component class using constructor and use the service class instance , Typescript Class: we do logical coding in typescript class and import in component class and use the typescript class instance. Then what is the difference between these two.It seems both are same Then why angular use service as special
Muhammed Albarmavi
correct if you compare simple class has no dependence but if the class (service) has dependence so you will have to pass all the dependence as example a service has dependence to HttpClinet with angular DI sytem angular will create the instance of the HttpCleint and pass this dependence to the service and create the service usualy some service has more than one dependence and that a huge benefite of using DI
Muhammed Albarmavi
second add any class to root level providers list make that service singleton mean this service will create once and this also anothe benefit ,service is the glue fo angular DI ,hope I give clear explination 😎 @VijayVJ