3

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?

4
  • 1
    Start 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? Commented 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. Commented Jun 11, 2019 at 11:11
  • 1
    From 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. Commented 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. Commented Jun 11, 2019 at 11:55

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

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
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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.