Rambo
Rambo

Reputation: 45

Angular 4 *ngFor loop parse value to directive as parameter

I have the following *ngFor loop and inside the loop I've got another div where I have applied a directive which expects an object parameter :

<div class="container" *ngFor="let item of items">
   <div [directiveName]="{testParamKey: item.name}"></div>
</div>

It gives me an error : "Cannot read property 'name' of undefined". I wonder how do i pass this parameter using the item from the for loop ?

Thanks in advance.

Upvotes: 0

Views: 195

Answers (1)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41407

use the optional operator. ?

<div [directiveName]="{testParamKey: item?.name}"></div>

Upvotes: 2

Related Questions