question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ngIf cryptic error when a then block is set to a non template

See original GitHub issue

Edit (vicb) - the error is caused by the ngIf

<p *ngIf="user.removed; else activeBlock">User had been removed: {{user.removed | date : 'dd.MM.yyyy'}}</p>
<p #activeBlock>User is active</p>

The second <p> should be a <ng-template> but the error message does not help

Hi. Probably i found an error.

I’ve used ngIf block inside ngFor block also inside ngFor block placed another ngFor.

Like this:

<tr *ngFor="let user of users">
    <td>
        <p *ngFor="let login of users.logins">{{login}}</p>
    </td>
    <td>
        <p *ngIf="user.removed; else activeBlock">User had been removed: {{user.removed | date : 'dd.MM.yyyy'}}</p>
        <p #activeBlock>User is active</p>
    </td>
</tr>

And this throws error: templateRef.createEmbeddedView is not a function

I’m using this code now (instead previous):

<tr *ngFor="let user of users">
    <td>
        <p *ngFor="let login of users.logins">{{login}}</p>
    </td>
    <td>
        <p *ngIf="user.removed">User had been removed: {{user.removed | date : 'dd.MM.yyyy'}}</p>
        <p  *ngIf="!user.removed">User is active</p>
    </td>
</tr>

Thanks for attention.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
tamasfoldicommented, Apr 28, 2017

Hi!

Try to put your else block into an <ng-template></ng-template> block as it shown in the changelog Like this:

<tr *ngFor="let user of users">
    <td>
        <p *ngFor="let login of users.logins">{{login}}</p>
    </td>
    <td>
        <p *ngIf="user.removed; else activeBlock">User had been removed: {{user.removed | date : 'dd.MM.yyyy'}}</p>
        <ng-template #activeBlock>
           <p>User is active</p>
       </ng-template>
    </td>
</tr>
3reactions
chai-jaycommented, Sep 26, 2017

@titusfx In short, yes.

The *ngIf directive is a structural directive with the asterisk character * being a syntatic sugar, and it expects you to pass <ng-template> for the else condition’s TemplateRef in the ngIf directive code to grab the referenced markup correctly. When you write <div *ngIf="showThis">Test</div>, it really becomes this:

<ng-template [ngIf]="showThis">
    <div>Test</div>
</ng-template>

And when you write this:

<div *ngIf="showThis; else thatTemplate>Test</div>
<ng-template #thatTemplate>
    <div>Else template</div>
</ng-template>

It gets desugared to something like this (I’m not 100% sure on this desugaring):

<ng-template [ngIf]="showThis" [ngIfElse]="thatTemplate">
    <div>Test</div>
</ng-template>
<ng-template #thatTemplate>
    <div>Else template</div>
</ng-template>

For more info, read these sections: https://angular.io/guide/structural-directives#what-are-structural-directives and https://angular.io/guide/structural-directives#the-asterisk--prefix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngIf getting error on block that get false - Stack Overflow
First of all fix your ngIf attribute with quotes: *ngIf="userData?.isAuth". The problem is, that | is used for pipes in templates and the ......
Read more >
Angular. ngIf then/else block shows the error (statement ...
i.e. there is no semicolon after thenblock. But https://angular.io/api/common/NgIf#using-non-inlined-then-template shows a different syntax. Both ...
Read more >
Why your Angular App is not Working: 11 common Mistakes
When your angular app is not working and all it gives you are some cryptic red lines in a console. Especially when you...
Read more >
NgIf - Angular
A structural directive that conditionally includes a template based on the value of an expression coerced to Boolean. When the expression evaluates to...
Read more >
Cisco Nexus 7000 Series NX-OS Release Notes, Release 8.x
VLAN translation on fabric extender is not supported. If you need to map a VLAN, you must move the interface to the parent...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found