Passing parent child in Angular2

How can nested components interact with each other in Angular 2?

Adding @Parent() to any level constructor violates the application.

Given the following code, how can I grab a message from GrandParent, Parent, Child, and GrandChild at each level?

/ client / grandparent

 import {Component, View, bootstrap} from 'angular2/angular2'; import {ParentCmp} from 'client/parent'; import {ChildCmp} from 'client/child'; import {GrandChildCmp} from 'client/grandchild'; @Component({ selector: 'grand-parent' }) @View({ template: ` <div style="background-color: lightgreen; padding: 10px;"> <h1>Grand-Parent</h1> <p>Parent: {{parentMessage}}</p> <p>Child: {{childMessage}}</p> <p>Grand-Child: {{grandChildMessage}}</p> <parent></parent> </div> `, directives: [ParentCmp] }) class GrandParent { message:string; parentMessage:string; childMessage: string; grandChildMessage: string; constructor() { this.message = "message from grandparent"; //this.parentMessage = parent.message; //this.childMessage = child.message; //this.grandChildMessage = grandChild.message; } } bootstrap(GrandParent); 

/ client / parent

 import {Component, View, Parent} from 'angular2/angular2'; import {GrandParentCmp} from 'client/grandparent'; import {ChildCmp} from 'client/child'; import {GrandChildCmp} from 'client/grandchild'; @Component({ selector: 'parent' }) @View({ template: ` <div style="background-color: lightblue; padding: 10px;"> <p>Grand-Parent: {{grandParentMessage}}</p> <h1>Parent</h1> <p>Child: {{childMessage}}</p> <p>Grand-Child: {{grandChildMessage}}</p> <child></child> </div> `, directives: [ChildCmp] }) export class ParentCmp { message:string; grandParentMessage:string; childMessage:string; grandChildMessage:string; constructor() { this.message = "message from parent"; //this.grandParentMessage = grandParent.message; //this.childMessage = child.message; //this.grandChildMessage = grandChild.message; } } 

/ client / child

 import {Component, View, Host} from 'angular2/angular2'; import {GrandParentCmp} from 'client/grandparent'; import {ParentCmp} from 'client/parent'; import {GrandChildCmp} from 'client/grandchild'; @Component({ selector: 'child' }) @View({ template: ` <div style="background-color: orange; padding: 10px;"> <p>GrandParent: {{grandParentMessage}}</p> <p>Parent: {{parentMessage}}</p> <h1>Child</h1> <p>Grand-Child: {{grandChildMessage}}</p> <grand-child></grand-child> </div> `, directives: [GrandChildCmp] }) export class ChildCmp { message:string; grandParentMessage:string; parentMessage:string; grandChildMessage:string; constructor() { this.message = "message from child"; //this.grandParentMessage = grandParent.message; //this.parentMessage = parent.message; //this.grandChildMessage = grandChild.message; } } 

/ client / grandchild

 import {Component, View, Host} from 'angular2/angular2'; import {GrandParentCmp} from 'client/grandparent'; import {ParentCmp} from 'client/parent'; import {ChildCmp} from 'client/child'; @Component({ selector: 'grand-child' }) @View({ template: ` <div style="background-color: lightgrey; padding: 10px;"> <p>Grand-Parent: {{grandParentMessage}}</p> <p>Parent: {{parentMessage}}</p> <p>Child</p> <h1>Grand-Child: {{grandChildMessage}}</h1> <child></child> </div> ` }) export class GrandChildCmp { message:string; grandParentMessage:string; parentMessage:string; grandChildMessage:string; constructor() { this.message = "message from grandchild"; //this.grandParentMessage = grandParent.message; //this.parentMessage = parent.message; //this.grandChildMessage = grandChild.message; } } 

I have included a link to the Github Repo .

+2
angular
source share

No one has answered this question yet.

See similar questions:

7
Component as injectable?

or similar:

46
Dynamic component selection in Angular2
29th
Angular2: insert a dynamic component as a child of a container in the DOM
7
Angular2 pass attribute to class constructor
3
Submit form from parent component using Angular
2
Angular2 delimiter breaks css parent / child relationship
one
Corner: message passing from parent to child does not work
one
Angular: parent-child relationship, @Host and forwardRef
one
How to introduce indirect ancestor component in Angular2
one
angular2 Child component, how to import var updated by parent component
0
Relationship between nested multiple components in Angular2

All Articles