Anna F Anna F - 1 year ago 46
TypeScript Question

How to pass variable from child to child in Angular2

I have three components : parent (P) and two children (C1) and (C2)
(they are both children of P component and they are not nested)
in parent template it looks like


I have a variable
(number), which I want decrease and increase in
component and give a new value of this variable back to
(so, I want to pass not a function, but variable). How could I pass this variable from
I would like to solve this problem without shared services.

Answer Source

You could emit the variable from child1 using a EventEmitter to the parent. Catch it as an input within the parent and then pass it to child2 within the selector.

Child 1:

import { Output } from '@angular/core';
@Output() testVariable = new EventEmitter();

Catch within parent and emit in Parent html:

<c2-component [testVariable]="testVariable"></c2-component>

Child 2:

import { Input} from '@angular/core';
@Input() testVariable = new EventEmitter();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download