ANGULAR 2/4 : call function for each row in NgFor

前端 未结 2 801
栀梦
栀梦 2021-01-25 23:29

i\'m learning AngularJS, and i need some help.

I have a template like this

    
相关标签:
2条回答
  • 2021-01-25 23:45

    Get an array as property on your class. Each time your function is called, set the corresponding item (say i) of this array to the value. And in the template reference the i item of that same array.

    0 讨论(0)
  • 2021-01-25 23:53
    this.data = dataFromSomewhere();
    this.dataOpt = this.data.map((d) => this.myFunction(d.id));
    
    <div *ngFor="let beat of data; let i=index"  class="item">
      <div>{{dataOpt[i]}}</div>
    

    The pipe variant:

    @Pipe({selector: myFunc})
    class MyPipy implements PipeTransform {
      transform(val:string) {
        return // do the same calculation here that you would do in `myFunction`;
      }
    }
    

    and use it like

    <div *ngFor="let beat of data; let i=index"  class="item">
      <div>{{data | myPipe}}</div>
    

    (the pipe needs to be registered in declarations of the module (or in an imported module)

    0 讨论(0)
提交回复
热议问题