jeudi 5 janvier 2017

Angular 2 Event Handling Communication Between Components

In this current Angular 2 application, I have five components. One parent component and four children components of the parent. The issue I'm having is with two of the children communicating click events on buttons. Child Component A has a function called when click event occurs on a button fires a function in the service to create a db instance. Child Component B also has the same functionality as A.

My question is how to aggregate the data between the two components because after both click events register the data they are representing, that chunk of data is what should be pushed into the database.

Proposed solutions:

  1. Observable of component B to push the data on an array, and pop it off as its been handled by the service. Though I'm not sure if there's a better solution out there. I have been looking for better walk through to help myself understand Observables, but all I can find is Observables with HTTP.

  2. Use outputs with event emitters on both children components and aggregate the data in the parent component. However, I'm not sure how to make the component wait for the second click event on the other component before calling the service to create the db entry.

Aucun commentaire:

Enregistrer un commentaire