Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I repeat content (aka *ngFor) without a wrapping element?

Tags:

angular

I have an Angular 4 component that uses what is effectively a 2d array. I have an array of sections which hold an array of links. I want to be able to output them all flatly:

<ul>
  <div *ngFor="let section of all_sections">
    <li *ngFor="let nav of section.links" [class.active]="nav.href === current_url">
    </li>
    <li class="divider"></li>
  </div>
</ul>

How can I force it to do the loops but without the extra wrapping div for the sections? It should just be li tags inside the ul.

Expected output:

<ul>
    <li class="active"></li>
    <li class="active"></li>
    <li class="active"></li>
    <li class="divider"></li>
    <li class="active"></li>
    <li class="active"></li>
    <li class="active"></li>
    <li class="divider"></li>
</ul>
like image 763
Nate Radebaugh Avatar asked Apr 13 '17 05:04

Nate Radebaugh


People also ask

How to iterate array in ngFor?

Introduction. *ngFor is a predefined directive in Angular. It accepts an array to iterate data over atemplate to replicate the template with different data. It's the same as the forEach() method in JavaScript, which also iterates over an array.

Is ngFor a loop?

Angular allows us to use a variety of useful directives. One of the most basic and common is ngFor which is an implementation of a for loop . ngFor is designed to work with collections. Since we have a collection of events let's see how to use the ngFor directive in our code.

How to use ngFor in angular?

To Use ngFor directive, you have to create a block of HTML elements, which can display a single item of the items collection. After that you can use the ngFor directive to tell angular to repeat that block of HTML elements for each item in the list.


Video Answer


1 Answers

you can try using ng-container

<ng-container *ngFor="let section of all_sections;">
 ...
</ng-container>
like image 61
Jayakrishnan Avatar answered Sep 18 '22 15:09

Jayakrishnan