Home > Net >  Angular 2 content projection share data between components
Angular 2 content projection share data between components

Time:08-01

i want to pass some data to the nested component from parent component, can anybody help me with this, thank you

<parent> <nested-comp></nested-comp> </parent>

CodePudding user response:

having

@Component()
class NestedComponent{

    @Input("someInput")
       private yourDataInput:any

}

<parent> <nested-comp [someInput]=>"someData"></nested-comp> </parent>

will allow you to pass data between those 2 components by setting binding someData into yourDataInput field

CodePudding user response:

You need to use @Input() in Angular, for example:

In parente component html you set a label or anything you need to pass to child component:

private label: string = 'Label Test';

In parent component html you set the parameter to nested component:

<app-child [label]="label"></app-child>

In nested component ts you declare:

@Input() label: string;

In nested component html you use:

<label>{{label}}</label>

Doing this you will be able to pass any data from your parent to child components in Angular.

  • Related