N
N
Natalia252017-07-26 21:16:15
JavaScript
Natalia25, 2017-07-26 21:16:15

How to pass variable from one child component to another in Angular2?

There are 3 components: parent (P) and 2 children (C1) and (C2) (C1 and C2 are both child components for P and they are not nested)
Template for P looks something like this

<c1-component></c1-component>
<c2-component></c2-component>

c1 has a score variable that needs to be passed to c2 where there is a count_score() method that will add or subtract numbers to it, and then the updated score variable needs to be passed back to c1 from c2 because the count_score() method will be called multiple times. times (accordingly, it will be necessary to add or subtract values ​​already with the updated variable). Tell me, please, how can I solve the problem?
As far as I understand, it is possible through:
1) Shared Service
2) Template and EventEmittler (it seems that this is the best way, I tried to do something, like the other 2 ways, but it doesn’t work out)
3) transfer from c1 to the parent, and from there descend to c2, then from c2 to the parent and from there to c1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2017-07-26
@bingo347

Business logic should be in services, and in components only UI, then there will be no such problems

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question