question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get Dynamic Gallery Images

See original GitHub issue

Hi @lukasz-galka, Can you please help me, I need to clarify, I’m just a bit confused as to How can we make getting Gallery Images dynamic? I find myself in a situation where I need to get the URL of images from the database.

Here’s what I got:

getGalleryImages() {
    this.galleryImages = [
      {
        small: `${product.images.AP.front}`,
        medium: `${product.images.AP.side}`,
        big: `${product.images.AP.back}`
      }
    ]
  }

But this definitely doesn’t work in the Component file, it’s not compiled because TypeScript doesn’t recognize what is AP, it returns undefined for AP except in the template.html like so {{ product.images.AP.front }} How can you help me achieve my goal of making this dynamic, either in the component file or in the template file?

BTW, here’s an example of what my Image Object from the database looks like:

"images": {
      "AP": {
        "side": "http://vendors.blankstyle.com/ap/data/Hi-Res%20Web%20Images/5066bt_sd_mg.jpg",
        "front": "http://vendors.blankstyle.com/ap/data/Hi-Res%20Web%20Images/5066bt_mg.jpg",
        "back": "http://vendors.blankstyle.com/ap/data/Hi-Res%20Web%20Images/5066bt_mg.jpg"
       }
}

Thanks in advance for your help and suggestions.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
lukasz-galkacommented, Oct 18, 2017

You have undefined in console.log because at this moment you don’t have product. Your code is like

  1. make async request to server for product
  2. console.log
  3. response from server

It is working in html because Angular is making change detection after every http request. So Angular knows that your response is end and is refreshing html. More here https://blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection-explained.html

In html it looks like

  1. make async request to server for product
  2. <img src="undefined"></img>
  3. response from server
  4. <img src="photo.jpg"></img>

Imo your code should look

  ngOnInit(): void {
    this.getProductRoute();
    this.getGalleryOptions();
    this.galleryImages = [];
  }

  getProductRoute() {
    this.route.paramMap
      .switchMap((params: ParamMap) => 
        this.productService.getProduct(+params.get('id')))
      .subscribe(product => {
          this.product = product;
         
          this.galleryImages = [{
              small: product.images.AP.front,
              medium: product.images.AP.side,
              big: product.images.AP.back
          }]
      );
  }

So you should create galleryImages array after response from server.

1reaction
mayordwellscommented, Oct 19, 2017

Thanks, everything is fine now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create Dynamic Image Gallery with Database using ...
Create Dynamic Image Gallery with Database using jQuery, PHP & MySQL · Create Database Table. To store image file information a table needs...
Read more >
Solved: How to dynamically change Gallery Image
Solved: Hi I've inserted a gallery that gets the data from a SharePoint List. I wanted to change the item image in the...
Read more >
dynamic image gallery - php - Stack Overflow
Very simple auto gallery script, photos.php: <?php function getDirTree($dir,$p=true) { $d ... the folder that get displayed is the $path.
Read more >
Dynamic galleries - Google Web Designer Help
Dynamic galleries. You can use groups to display multiple dynamic elements within each frame of a Swipeable Gallery or Carousel Gallery. For example,...
Read more >
How To Create A Dynamic Gallery In WordPress - FooPlugins
A dynamic gallery is a photo gallery on your site, where the images update to show new content based on changes made to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found