Services Flashcards

1
Q

What is a Service?

A

It is a functionality that can be shared with different components

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

What is the functionality of Service?

A

When there are common aspects in the components then we separate it out and include them in Service.
When the functionality is needed for the component, include the service in the component.

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

How to inject the Service?

A

@Injectable decorator is used on the service class.

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

Why should Service be registered?

A

For using the Service in the Component, it has to be registered.

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

Where is the Service registered?

A

Service can be registered at any level component. If it has to be used by all the components, it has to be registered at root level.
Service will be available for the parent and the child only

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

How to inject a Service in component?

A

Service can be injected as a parameter to the constructor class.
constructor(private ingr: Ingredients){
}
Now the methods of the Service can be executed.

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

Is it good to have the Service in the constructor as parameter if Service has to pull data

A

No, constructor has to wait till the time data is pulled.

Implement OnInit interface and call the service method inside ngOnInit() function.

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

How is the Service registered?

A
By adding the @NgModule 
@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  providers:[ProfileService],
  bootstrap: [ App ]
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly