user11081980
user11081980

Reputation: 3289

Angular and nested components: where should I inject the data-retrieving services?

I'm having trouble designing components in Angular 4.

Given a parent component with many children components, I'm trying to inject the services that retrieve data from the API only to the parent component. In other words, I avoid inject the data-retrieving services to the children components.

Instead, I let the parent component query the data and send it to the children components as input parameters. When the children components need to modify the data, I will bubble up events (output parameters) to the parent component and I will let it use the injected service to modify the data.

This is testable, but I'm doing this out of intuition. Are there any guidelines on how to design components based on this scenario?

Upvotes: 0

Views: 316

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222682

You are on the right track! If your child components are going to display/edit the same data that is retrieved in the parent component , event emitters are the best option.

Another option would be to use a shared service across the components, you initially retrieve the data and pass the data across the components, inorder to edit you could use shared methods.

Upvotes: 1

Related Questions