Not sure why my *ngFor loop is printing nothing out. I have the following code in an html file:
<table class="table table-hover"> <thead> <tr> <th>Name</th> <th>Email</th> <th>Company</th> <th>Status</th> </tr> </thead> <tbody> <!-- NGFOR ATTEMPTED HERE -- no content printed --> <ng-template *ngFor="let xb of tempData"> <tr data-toggle="collapse" data-target="#demo1" class="accordion-toggle"> <td>{{ xb.name }}</td> <td>{{ xb.email }}</td> <td>{{ xb.company }}</td> <td>{{ xb.status }}</td> </tr> <!-- other content --> </ng-template> </tbody> </table>
Then, in my simple component I have the following:
import { Component } from '@angular/core'; @Component({ selector: 'my-profile-exhibitors', templateUrl: './profile-exhibitors.component.html', styleUrls: ['./profile-exhibitors.component.scss'] }) export class ProfileExhibitorsComponent { public tempData: any = [ { 'name': 'name1', 'email': 'email1@gmail', 'company': 'company', 'status': 'Complete' }, { 'name': 'name2', 'email': 'email2@gmail', 'company': 'company', 'status': 'Incomplete' } ]; constructor() {} }
When I run this code, I get zero output. Even weirder is that when I select the element using debug tools I see this:
Looks like it correctly recognizes my object, but then outputs nothing.
When we use *ngFor , we're telling Angular to essentially treat the element the * is bound to as a template. Angular's <ng-template> element is not a true Web Component (unlike <template> ).
ng-template is an Angular element that is used for rendering HTML in a template. However, it is not rendered directly on DOM. If you include an ng-template tag to a template, the tag and the content inside it will be replaced by comment upon render.
We can pass the entire template to a child component from the parent component. The technique is similar to passing data from parent to child component. Create a parent component. Add a ng-template and name it as #parentTemplate .
Descriptionlink. The ngForOf directive is generally used in the shorthand form *ngFor . In this form, the template to be rendered for each iteration is the content of an anchor element containing the directive. The following example shows the shorthand syntax with some options, contained in an <li> element.
I think what you want is
<ng-container *ngFor="let xb of tempData">
or
<ng-template ngFor let-xb [ngForOf]="tempData">
For getting index as well: <ng-template ngFor let-xb [ngForOf]="tempData" let-index="index">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With