3

I have an ngFor directive like this:

<div *ngFor="let data of datas|slice:0:2"><app-data [data]="data"></app-data></div>

But the data object is not getting passed into the app-data component from the enclosing div.

How do I pass data from the div to app-data?

4
  • This should work, have you tried to console log the data object? Commented Nov 2, 2020 at 5:55
  • I'm getting a NaN. Not even undefined. Commented Nov 2, 2020 at 6:04
  • Please share the code where you define data Commented Nov 2, 2020 at 6:05
  • @Latcie please check my below answer and if u clear with my answer that vote up and mark as good answers it will help to other. Commented Nov 2, 2020 at 6:10

1 Answer 1

2

Parent Component


You can use Simply

For Example I have parent Component like this

<div *ngFor="let data of datas|slice:0:2">
    <app-child [data]="data"></app-child>
</div>

TS

I have Dummy array

 datas = [
    {id: 1, name: 'ABC'},
    {id: 2, name: 'EFG'}
  ];

Child Component


now you can use this array to child component like this

HTML

<ul>
    <li>{{data.id}}</li>
    <li>{{data.name}}</li>     
</ul>

TS

// This will bring data from parent component and this is used for sharing data between parent context and child directives or components
@Input() data;

ngOnInit() {
  console.log('data => ', this.data);
}

My Example you can check it Here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.