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 @Input
s?
If so, how does we know which data comes into which @Input
? Order matters?
If not, how to achieve that?
Sorry if I miss basic point in this.
Thanks..
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.
@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.
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.
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.
You just create public variables with @Input() attribute:
export class ExampleComponent{
@Input('exData1') exData1: any;
@Input('exData2') exData2: any;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With