最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - angular 2 display array of images - Stack Overflow

programmeradmin4浏览0评论

i am new to angular 2 , i have an array of images and i want to display all the images in a row but none of them is being displayed even the array is not empty i am using the img tag as shown in the code

html code:

<div fxLayout="column">


    <div >

    </div>

    <div fxLayout="row" *ngIf="doneLoadingProjectData" >
      <div  *ngFor="let image of images" >
          <img src='image'/>
      </div>

    </div>


</div>

i am new to angular 2 , i have an array of images and i want to display all the images in a row but none of them is being displayed even the array is not empty i am using the img tag as shown in the code

html code:

<div fxLayout="column">


    <div >

    </div>

    <div fxLayout="row" *ngIf="doneLoadingProjectData" >
      <div  *ngFor="let image of images" >
          <img src='image'/>
      </div>

    </div>


</div>

Share Improve this question edited Mar 29, 2017 at 3:22 mohammad asked Mar 29, 2017 at 3:10 mohammadmohammad 2,2127 gold badges37 silver badges62 bronze badges 2
  • 1 ngIf and ngFor can't be used in the same element. Check out this solution for ng-container workaround: stackoverflow./questions/34657821/… – Z. Bagley Commented Mar 29, 2017 at 3:13
  • If [src] is not worked, you have to Sanitize the url. – Harish Kommuri Commented Mar 29, 2017 at 5:14
Add a ment  | 

2 Answers 2

Reset to default 8

Need to use curly brackets when you are assigning value to src attribute

<div  *ngFor="let image of images" >
  <img src='{{image}}'/>
</div>

Or you can use a template expression:

<img [src]="image" />

Also I believe that the array of images needs to be array (better to use JSON)of links to the images only then will the image show up on the browser when ngFor is used to display each image

<div *ngIf="images">
<ul>
  <li *ngFor="let image of images">
        <img [src]="image.href" />
  </li>
</ul>

Also I think Maybe a better would be to use ngOnInit so that all initialization is done when that div is called

<div ngOnInit>
<ul>
  <li *ngFor="let image of images">
        <img [src]="image.href" />
  </li>
</ul>

And in the ponent you can run your code as you wish

 images = [];
 ngOnInit() { type your code here it is similar to a constructor, here you can call your service or factory to get array of images you can initialize images here }

Where the image array is something like this

{ "images" : [ { "name" : "image1", "href" : "http://east-nj1.photos.example/987/nj1-1234" }, { "name" : "image2", "href" : "http://east-nj1.photos.example/987/nj1-1234" }, { "name" : "image3", "href" : "http://east-nj1.photos.example/987/nj1-1234" }, { "name" : "image4", "href" : "http://east-nj1.photos.example/987/nj1-1234" } ] }

Copy Paste this blockquote in this link you will get an idea how the data is passed link you can access each property of the JSON value as image.name and image.href. Hope you find this usefull

发布评论

评论列表(0)

  1. 暂无评论