Home > Enterprise >  Why doesn't my else work with ngIF in Agular 12?
Why doesn't my else work with ngIF in Agular 12?

Time:06-27

I am brushing up my Angular knowledge and I came across an "else" to use if ngIf. In reality I think it is easier to use "valor !== false" to simulate an else. But if I need to give assistance in a code that uses I want to be prepared.

<div *ngIf="valor === 'teste'; else showOther">tttt</div>

<ng-temlate #showOther>
  <ng-content> outro </ng-content>
</ng-temlate>

I get this

Type 'HTMLElement' is not assignable to type 'TemplateRef<NgIfContext<boolean>>'.

The code

CodePudding user response:

You wrote ng-temlate instead of ng-template.

But after fixing it, I still get this error:

Uncaught Error: Template parse errors: element cannot have content. (" <div *ngIf="valor === 'teste' ; else showOther">Hello! <ng-template #showOther> [ERROR ->]Content to render when condition is false.

Remove the ng-content tag, it will work. So the code should be:

<div *ngIf="valor === 'teste' ; else showOther">ttt</div>
<ng-template #showOther>
  Content to render when condition is false.
</ng-template>
  • Related