4

I am creating a simple app to test some stuff using observables and for some reason, when I wrap that code using an ng-template tag, the HTML simply doesn't show up.

The reason I'm using this ng-template before the div, is to use an *ngIf. Initially, I thought that the *ngIf was the problem, but when I removed that statement from the template, it still did not work. So the If is not the problem.

Any idea?

<!-- If I remove the <ng-template> tag, the HTML is rendered, but with it, it doesn't work -->
<ng-template>
    <div class="messages-container" *ngIf="(messagesService.errors$ | async) as errors">
        <div class="messages" *ngFor="let error of errors">{{error}}</div>
        <mat-icon class="close" (click)="onClose()">close</mat-icon>
    </div>
</ng-template>

Edit: When I use [ngIf] instead of *ngIf it works, but I still don't understand why works whit [ngIf] and why it does not work with no condition.

1 Answer 1

4

star syntax in structural directives like here *ngIf implicitly creates a template from the element that this directive is being used on. no need to explicitly write <ng-temlate> just use *ngIf

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

6 Comments

Can you give me an example?
just remove <ng-template> from you code, and it should work just fine
But that's the point, why I can't use the <ng-template> with the start syntax nor without any structural directive?
If I change it to <ng-container> works fine
because by using <ng-template> you are creating a rule of how to create a piece of html which then you can render somewhere. but you are not rendering it anywhere. If you add [ngIf] directive to <ng-template> element, then this directive executes the logic of rendering this template 1 time. if you use [ngForOf]... on this template then it will be rendered multiple times
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.