Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

*ngFor on ng-template outputs nothing Angular2

Tags:

angular

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:

enter image description here

Looks like it correctly recognizes my object, but then outputs nothing.

like image 985
Syntactic Fructose Avatar asked Apr 03 '17 06:04

Syntactic Fructose


People also ask

Can we use ngFor in ng-template?

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> ).

Does ng-template get rendered?

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.

Can we pass data to ng-template?

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 .

What is ngForOf?

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.


2 Answers

I think what you want is

<ng-container *ngFor="let xb of tempData"> 

or

<ng-template ngFor let-xb [ngForOf]="tempData"> 
like image 106
Günter Zöchbauer Avatar answered Sep 23 '22 18:09

Günter Zöchbauer


For getting index as well: <ng-template ngFor let-xb [ngForOf]="tempData" let-index="index">

like image 44
SUSEENDHAR LAL Avatar answered Sep 22 '22 18:09

SUSEENDHAR LAL