Angular image conditionally with or without anchor-tag

Viewed 123

I have this situation in a component now:

  <a [routerLink]="[pageUrl] | localize" *ngIf="pageUrl">
    <img />
  </a>
  <ng-container *ngIf="!pageUrl">
    <img />
  </ng-container>

Ofcourse the image-tag can be much more. The thing is: I don't want to repeat myself. Can I solve this with an ng-template or something like that, and how would this work?

1 Answers

yes, it can be done with ng-template for else conditioning as below

 <a [routerLink]="[pageUrl] | localize" *ngIf="pageUrl; else elseBlock">
    <img />
  </a>
<ng-template #elseBlock>Content for else block</ng-template>
Related