Services Flashcards
What is a Service?
It is a functionality that can be shared with different components
What is the functionality of Service?
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 to inject the Service?
@Injectable decorator is used on the service class.
Why should Service be registered?
For using the Service in the Component, it has to be registered.
Where is the Service registered?
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 to inject a Service in component?
Service can be injected as a parameter to the constructor class.
constructor(private ingr: Ingredients){
}
Now the methods of the Service can be executed.
Is it good to have the Service in the constructor as parameter if Service has to pull data
No, constructor has to wait till the time data is pulled.
Implement OnInit interface and call the service method inside ngOnInit() function.
How is the Service registered?
By adding the @NgModule @NgModule({ imports: [ BrowserModule ], declarations: [ App ], providers:[ProfileService], bootstrap: [ App ] })