Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 8 Display image src stored as ng model in variable

I want to display image src as dynamic variable, I declared a default value as string in variable named displayedImage :

<div class="col-6 " style="margin-top: 35px;">
  <div class="row">
    <div class="col-6">
      <img src="assets/cardsModels/Anniversaire.png" alt="">
    </div>
  </div>
</div>

**

this.displayedImage = 'assets/cardsModels/Anniversaire.pnf'

this.displayedImage declared in the constructor, the image not found and cannot be displayed !

like image 243
sahnoun Avatar asked Dec 22 '22 20:12

sahnoun


2 Answers

The solution to your question is to change html like (using data binding):

<div class="col-6 " style="margin-top: 35px;">
  <div class="row">
    <div class="col-6">
      <img [src]="displayedImage" alt="">
    </div>
  </div>
</div>

And fix the typo from:

this.displayedImage = 'assets/cardsModels/Anniversaire.pnf'

to:

this.displayedImage = 'assets/cardsModels/Anniversaire.png'
like image 97
Nikolay Zatonatskiy Avatar answered Dec 25 '22 10:12

Nikolay Zatonatskiy


just use your variable displayedImage in src for an image:

<div class="col-6 " style="margin-top: 35px;">
  <div class="row">
    <div class="col-6">
      <img [src]="displayedImage" alt="">
    </div>
  </div>
</div>

or you can use curly braces {{ displayedImage }}:

<div class="col-6 " style="margin-top: 35px;">
  <div class="row">
    <div class="col-6">
        <img src={{displayedImage}} />
    </div>
  </div>
</div>
like image 35
StepUp Avatar answered Dec 25 '22 10:12

StepUp