NG Interview Set I - Services Questions Flashcards

NG Interview Set I - Services Questions

1
Q

What is the use case of services?

A
  • One very common use case is providing data to components, often by fetching it from a server. Though there’s no real definition of service from Angular point of view – it could do almost anything (e.g., logging is another common use case, among many)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are the services injected to your application?

A
  • Via Angular’s DI (Dependency Injection) mechanism
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is it a bad idea to create a new service in a component like the one below?

let service = new DataService();

A
  • The object may not be created with its needed dependencies
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to make sure that a single instance of a service will be used in an entire application?

A
  • Provide it in the root module
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why do we need provider aliases? And how do you create one?

A
  • To substitute an alternative implementation for a provider. Can create like so:

{ provide: LoggerService, useClass: DateLoggerService }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly