Ng-repeat-start in angular2 - aka repeat multiple elements using NgFor

前端 未结 4 1196
Happy的楠姐
Happy的楠姐 2020-12-15 04:15

I need to repeat several li-elements in a list in Angular2 for each item. In angular 1.x I used ng-repeat-start and ng-repeat-end for

相关标签:
4条回答
  • 2020-12-15 04:32

    In the newer versions it works like this:

    <ul class="dropdown-menu" role="menu">
      <template ngFor let-category [ngForOf]="categories">
        <li class="dropdown-header">
            {{ category.title }}
        </li>
        <li>
            <a href="{{ '/music/' + tag.keyword }}" *ngFor="#tag of category.tags" [hidden]="tag.deleted === 1">{{ tag.tag_title_da }}</a>
        </li>
        <li class="divider"></li>
    
        <li class="dropdown-header">Alle musikstykker</li>
        <li><a href="/music/all">Alle musikstykker</a></li>
      </template>
    </ul>
    

    --> let-category instead of #category

    0 讨论(0)
  • 2020-12-15 04:32

    Thanks for your effort, pixelbits, however the answer was different.

    Reading about the asterisk in Template Syntax in the Angular.io guides gives you the clue.

    This solved it:

    <template ngFor #category [ngForOf]="categories">
        <li class="dropdown-header">
            {{ category.title }}
        </li>
        <li>
            <a href="{{ '/music/' + tag.keyword }}" *ngFor="#tag of category.tags" [hidden]="tag.deleted === 1">{{ tag.tag_title_da }}</a>
        </li>
        <li class="divider"></li>
    </template> 
    
    0 讨论(0)
  • If you want to repeat the contents, use the template tag, and remove the * prefix on ngFor.

    According to Victor Savkin on ngFor and templates:

    Angular treats template elements in a special way. They are used to create views, chunks of DOM you can dynamically manipulate. The * syntax is a shortcut that lets you avoid writing the whole element.

    <ul class="dropdown-menu" role="menu">
       <template ngFor #category [ngForOf]="categories">
        <li class="dropdown-header">
            {{ category.title }}
        </li>
        <li>
            <a href="{{ '/music/' + tag.keyword }}" *ngFor="#tag of category.tags" [hidden]="tag.deleted === 1">{{ tag.tag_title_da }}</a>
        </li>
        <li class="divider"></li>
    
        <li class="dropdown-header">Alle musikstykker</li>
        <li><a href="/music/all">Alle musikstykker</a></li>
        </template>
    </ul>
    

    Update angular ^2.0.0

    You can use ng-container and just change #var to let var.

    <ng-container> behaves the same as the <template> but allows to use the more common syntax.

    <ul class="dropdown-menu" role="menu">
      <ng-container *ngFor="let category of categories">
        <li class="dropdown-header">
          {{ category.title }}
        </li>
        <li>
          <a href="{{ '/music/' + tag.keyword }}" *ngFor="let tag of category.tags" [hidden]="tag.deleted === 1">{{ tag.tag_title_da }}</a>
        </li>
        <li class="divider"></li>
    
        <li class="dropdown-header">Alle musikstykker</li>
        <li><a href="/music/all">Alle musikstykker</a></li>
      </ng-container>
    </ul>
    
    0 讨论(0)
  • 2020-12-15 04:45

    Worked with 5.2.x

    <ng-template ngFor let-category [ngForOf]="categories">
      <label class="value-fields_label">{{ category.name }}</label>
      <h3 class="value-fields_value">{{ category.title }}</h3>
    </ng-template>
    
    0 讨论(0)
提交回复
热议问题