You can use a custom directive to call a method for each iteration:
import { Directive, Output, EventEmitter, Input, SimpleChange} from '@angular/core'; @Directive({ selector: '[onCreate]' }) export class OnCreate { @Output() onCreate: EventEmitter<any> = new EventEmitter<any>(); constructor() {} ngOnInit() { this.onCreate.emit('dummy'); } }
and then you can use it in your * ngFor to call a method in your component:
<div *ngFor="let item of items"> <div *ngIf="item" (onCreate)="yourMethod(item)"> </div> </div>
in your component, you can call this method as:
yourMethod(item){ console.log(item); }
source share