27

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 this. I can't find the right way to do it in Angular 2. There are some older blog posts about this, but their suggestions don't work in the newest beta of Angular2.

All <li>-elements should be repeated for each category: (which I would normally do with the attribute *ngFor="#category of categories" - but I can't find where to put it...

Help?

<ul class="dropdown-menu" role="menu">
    <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>
</ul>
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
skovmand
  • 4,372
  • 5
  • 25
  • 30

4 Answers4

54

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>
yurzui
  • 205,937
  • 32
  • 433
  • 399
Michael Kang
  • 52,003
  • 16
  • 103
  • 135
  • 1
    Yeah, but I need to repeat all five ```
  • ``` for each category, and I can't put a ```
    ``` in to wrap them without destroying the list...
  • – skovmand Dec 30 '15 at 16:33
  • The ```
  • ...
  • ``` you put in is another repeat inside the first repeat, that should work well. – skovmand Dec 30 '15 at 16:35
  • If you put the parent `ngFor` on the `
      ` tag, it will repeat the categories.
    – Michael Kang Dec 30 '15 at 16:39
  • For your edit: I don't want the
      to repeat. Only its contents
    – skovmand Dec 30 '15 at 16:39
  • Maybe it should be: ` – Lodewijk Bogaards Dec 30 '15 at 16:53
  • Is this still working like that (2.0.0-rc.1)? I can't access the properties of category inside the loop. – user1879408 May 17 '16 at 08:23
  • Can you tell why this isn't working? http://plnkr.co/edit/0e796DfClochN9tmKrMO?p=preview – Dan Cancro May 21 '16 at 18:11
  • Works perfectly in Angular 4. Thank you! – Liran H May 07 '19 at 12:27