0

One of a project where I'm handling is having below code.Can you tell me what it does? I know about *ngFor and *ngIf.But what are these [ngForOf]="topicdata" and ngFor let-topic? Can I simplify below code in a better way?

 <ng-template ngFor let-topic [ngForOf]="topicdata">
        <topic *ngIf="topic.num_of_definitions>0" [data]="topic"></topic>
 </ng-template>

I would like to have it as shown below.

<topic *ngFor="let topic of topicdata" [data]="topic" 
*ngIf="topic.num_of_definitions>0"></topic>

But then it shows this error:

[Angular] Can't have multiple template bindings on one element. Use only one attribute named 'template' or prefixed with *

3

2 Answers 2

4

The *ngFor directive is something Angular calls microsyntax. Microsyntax essentially gives developers a way of configuring a directive in a more compact and simple way. You can see that something is using the microsyntax for the preceding *.

The code:

<topic *ngFor="let topic of topicdata"> </topic>

Will for example be equal to:

<ng-template ngFor let-topic [ngForOf]="topicdata">
   <topic> ... </topic>
</ng-template>

Like in the example you have in your question.

So in short - you can replace you code with *ngFor="let topic of topicdata" and get the same result.

EDIT - TO FIX: *ngIf on the same line

Angular doesn't allow you to use two or more structural directives on the same element. So as a way of using *ngIfon the same line I recommend looping out an <ng-container> element and put your <topic> inside that one.

<ng-container *ngFor="let topic of topicdata">
   <topic *ngIf="topic.num_of_definitions > 0"> ... </topic>
<ng-container>

More in on ng-container here

Sign up to request clarification or add additional context in comments.

3 Comments

This is working <topic *ngFor="let topic of topicdata" [data]="topic"></topic>.But thi is not.Why? <ng-template *ngFor="let topic of topicdata"> <topic [data]="topic"></topic> </ng-template>
That's because the *ngFor will create the ng-template for you behind the scenes - so putting a *ngFor on an ng-template wouldn't make sense. Go for <topic *ngFor="let topic of topicdata" [data]="topic"></topic> :)
I can't do that hence *ngIf="topic.num_of_definitions>0"also there.please see update above.So how can I do that?
0

It's just a different syntax of ngFor. let-topic [ngForOf]="topicdata" creates a topic variables from topicdata array. You can just write it as follows which creates the syntax you used (with ng-template)

<div *ngFor="let topic of topicdata">
    // some template
</div>

You can find more here https://angular.io/guide/structural-directives#inside-ngfor

3 Comments

Yes, I would like this syntax.I'll try this and come back.Thanks.
This is working <topic *ngFor="let topic of topicdata" [data]="topic"></topic>.But thi is not.Why? <ng-template *ngFor="let topic of topicdata"> <topic [data]="topic"></topic> </ng-template>
Yes it is actually my bad. You can't use this syntax with ng-template. Because *ngFor actually sugared version of ng-template ngForOf. You can find more here angular.io/guide/structural-directives#inside-ngfor

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.