Call a function inside ngFor in angular2

后端 未结 9 1865
眼角桃花
眼角桃花 2020-12-06 05:08

Hello I am using Angular2 and wanted to fetch the server and get some values for each ID I get inside ngFor.

9条回答
  •  渐次进展
    2020-12-06 06:08

    You can make use of custom directive to call the method for each iteration:

    import { Directive, Output, EventEmitter, Input, SimpleChange} from '@angular/core';
    
    @Directive({
      selector: '[onCreate]'
    })
    export class OnCreate {
    
      @Output() onCreate: EventEmitter = new EventEmitter();
      constructor() {}
      ngOnInit() {      
         this.onCreate.emit('dummy'); 
      } 
    
    }
    

    and then you can use it in your *ngFor to call the method in your component:

    in Your component you can call this method as:

    yourMethod(item){
       console.log(item);
    }
    

提交回复
热议问题