简体   繁体   中英

How to dynamically call all the images and display using *ngFor in Angular?

I have many images inside an assets folder which I'm trying to display using *ngFor

*.ts

this.assetsImages = ['12.jpg', 'tt.jpg', 'an.jpg', 'pn.png'];

.html

<div *ngFor="let image of assetsImages" >
      <img src="../../assets/{{image}}" height="100px" width="100px" >
</div>

Above I'm statically writing path of the images to display. But if I have a huge number of images I'd need to hard-code all paths which is not feasible.

Is there any alternate way to display all images dynamically?

You can't do that with frontend. What you need to is using your back-end and return file in it.

try this

.ts

this.assetsImages = ['../../assets/12.jpg', '../../assets/tt.jpg', 'an.jpg', '../../assets/pn.png'];

html

<div *ngFor="let image of assetsImages" >
      <img [src]="image" height="100px" width="100px" >
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM