Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple data to child component using @Input

Tags:

angular

I have a question that if we want to pass multiple data to child component using @Input how to achieve that.

If we have component something like this:

<ex-comp [exData1]="exampleData1" [exData2]="exampleData2"></ex-comp>

How to get the data in the child component. Using two @Inputs?

  1. If so, how does we know which data comes into which @Input? Order matters?

  2. If not, how to achieve that?

Sorry if I miss basic point in this.

Thanks..

like image 464
Sai Avatar asked Jul 31 '17 06:07

Sai


People also ask

How can we pass data from parent to all child components?

To let Angular know that a property in a child component or directive can receive its value from its parent component we must use the @Input() decorator in the said child. The @Input() decorator allows data to be input into the child component from a parent component.

What is the difference between @input and @output in Angular?

@Input() and @Output() give a child component a way to communicate with its parent component. @Input() lets a parent component update data in the child component. Conversely, @Output() lets the child send data to a parent component.

How do you pass data into App components?

Firstly, we have to create a custom property to pass the data into a component. This can be done via input binding, which passes data from one component to another, generally from parent to child. This custom input binding is created by using the @Input() decorator.


2 Answers

You can achieve that in your child component by this

 @Input()exData1;
 @Input()exData2;

<ex-comp [exData1]="exampleData1" [exData2]="exampleData2"></ex-comp>

Here exampleData1 and exampleData2 are data from your parent component and exData1 and exData2 are the input names that you can access into your child component by above given code.

like image 149
Vivek Doshi Avatar answered Oct 16 '22 22:10

Vivek Doshi


You just create public variables with @Input() attribute:

export class ExampleComponent{

   @Input('exData1') exData1: any;
   @Input('exData2') exData2: any;
}
like image 37
Vano Maisuradze Avatar answered Oct 16 '22 23:10

Vano Maisuradze