How to create variable in ngFor loop?

后端 未结 4 1008
梦如初夏
梦如初夏 2020-12-03 06:49

I am trying to find out how to create a variable in an ngFor loop.

I have a loop like this:


  &l         


        
相关标签:
4条回答
  • 2020-12-03 07:06

    I think this is a classic case for making a subcomponent.

    <td *ngFor="#prod of products">
        <subComp [prod]=prod></subComp>
    </td>
    

    Your component would then have a prod input and run the required function once in OnInit.

    Simple example plunk here

    0 讨论(0)
  • 2020-12-03 07:14

    With angular 4 you can do this:

    <td *ngFor="#prod of products">
      <div *ngIf="getBuild(branch,prod); let build">
        <a href="{{build.url}}">
         {{build.status}}
        </a>
      </div>
    </td>
    
    0 讨论(0)
  • 2020-12-03 07:15

    No, just cache the result in getBuild for each branch/prod combination or as long as it is called with the same values as before.

    0 讨论(0)
  • 2020-12-03 07:17

    I think local variables (defined with the # character) don't apply for your use case.

    In fact, when you define a local variable on an HTML element it corresponds to the component if any. When there is no component on the element, the variable refers to the element itself.

    Specifying a value for a local variable allows you to select a specific directive associated with the current element. For example:

    <input #name="ngForm" ngControl="name" [(ngModel)]="company.name"/>
    

    will set the instance of the ngForm directive associated with the current in the name variable.

    So local variables don't target what you want, i.e. setting a value created for the current element of a loop.

    If you try to do something like that:

    <div *ngFor="#elt of eltList" >
      <span #localVariable="elt.title"></span>
      {{localVariable}}
    </div>
    

    You will have this following error:

    Error: Template parse errors:
    There is no directive with "exportAs" set to "elt.title" ("
      <div *ngFor="#elt of eltList" >
        <span [ERROR ->]#localVariable="elt.title"></span>
        {{localVariable}}
      </div>
    "): AppComponent@2:10
    

    Angular2 actually looks for a directive matching the provided name elt.title here)... See this plunkr to reproduce the error: https://plnkr.co/edit/qcMGr9FS7yQD8LbX18uY?p=preview

    See this link: http://victorsavkin.com/post/119943127151/angular-2-template-syntax, section "Local variables" for more details.

    In addition to the current element of the iteration, ngFor only provides a set of exported values that can be aliased to local variables: index, last, even and odd.

    See this link: https://angular.io/docs/ts/latest/api/common/NgFor-directive.html

    What you could do is to create a sub component to display elements in the loop. It will accept the current element as parameter and create your "local variable" as attribute of the component. You will be able then to use this attribute in the template of the component so it will be created once per element in the loop. Here is a sample:

    @Component({
      selector: 'elt',
      template: `
        <div>{{attr}}</div>
      `
    })
    export class ElementComponent {
      @Input() element;
    
      constructor() {
        // Your old "localVariable"
        this.attr = createAttribute(element.title);
      }
    
      createAttribute(_title:string) {
        // Do some processing
        return somethingFromTitle;
      }
    }
    

    and the way to use it:

    <div *ngFor="#elt of eltList" >
      <elt [element]="elt></elt>
    </div>
    
    0 讨论(0)
提交回复
热议问题