Marcel
Marcel

Reputation: 391

Use Property Binding in *ngIf

I have an Array which contains the information if an Offer has an image name. With the *ngIf it should show either the image or "No Images". But the Property Binding within *ngIf doesn't work. Is this even possible?

<div class="m-c-o" *ngFor="let offer of offers">

    <div class="m-c-p" *ngIf="showOffers">
      <div class="m-c-img-c">
        <img *ngIf="{{ offer.U_D__IMAGE }} != false" src="{{ imagePath + offer.U_D__IMAGE }}">
        <div *ngIf="{{ offer.U_D__IMAGE }} === false">
          <i class="materials-icons">photo_camera</i>
          <div class="m-c-img-ni">No Images</div>
        </div>
      </div>
    </div>

 </div

Upvotes: 1

Views: 16230

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

*ngIf directive doesn't need interpolation

<div class="m-c-img-c">
    <img *ngIf="offer.U_D__IMAGE" [src]="imagePath + offer.U_D__IMAGE">
    <div *ngIf="!offer.U_D__IMAGE">
      <i class="materials-icons">photo_camera</i>
      <div class="m-c-img-ni">No Images</div>
    </div>
</div>

You could make it more better using *ngIf else

<div class="m-c-img-c">
    <img *ngIf="offer.U_D__IMAGE else noResults" [src]="imagePath + offer.U_D__IMAGE" />
    <ng-template #noResults>
       <div>
         <i class="materials-icons">photo_camera</i>
         <div class="m-c-img-ni">No Images</div>
       </div>
    </ng-template>
</div>

Upvotes: 7

Related Questions