Home Angular 8 Angular 9 – Angular Ngx-Lightbox

Angular 9 – Angular Ngx-Lightbox

by therichpost
Published: Updated: 11 comments
Angular Lightbox Working Tutorial

Hello to all, welcome to therichpost.com. In this post, I will tell you about, Angular 9 – Angular Ngx-Lightbox.

angular lightbox
angular lightbox

Angular 14 came and if you are new then you must check below link:

  1. Angular14 Basic Tutorials

Here are the working commands and code snippet, please follow carefully:

1. Here is the command you need to run to install lightbox module into your angular 9 application:

npm i ngx-lightbox

 

2. Here is the code you need to add your angular.json file:

...
{
  "styles": [
    "node_modules/ngx-lightbox/lightbox.css",
    ...
  ],
}
...

 

3. Here is the code you need to add into your app.module.ts file:

import { LightboxModule } from 'ngx-lightbox';
 
@NgModule({
  imports: [ LightboxModule ]
})

 

4. Here is the code you need to add into your app.component.ts file:

import { Lightbox } from 'ngx-lightbox';
 
export class AppComponent {
 _albums:any = [];
  constructor(private _lightbox: Lightbox) {
    for (let i = 1; i <= 3; i++) {
      const src = 'https://therichpost.com/wp-content/uploads/2021/05/bootstrap5-carousel-slider-img' + i + '.jpg';
      const caption = 'Image ' + i + ' caption here';
      const thumb = 'https://therichpost.com/wp-content/uploads/2021/05/bootstrap5-carousel-slider-img' + i + '.jpg';
      const album = {
         src: src,
         caption: caption,
         thumb: thumb
      };
 
      this._albums.push(album);
    }
  }
 
  open(index: number): void {
    // open lightbox
    this._lightbox.open(this._albums, index);
  }
 
  close(): void {
    // close lightbox programmatically
    this._lightbox.close();
  }
}

5. Here is the code you need to add into your app.component.html file:

<div *ngFor="let image of _albums; let i=index">
  <img width="100" [src]="image.thumb" (click)="open(i)" />
</div>

 

This is it and if you have any kind of query then do comment below.

Jassa

Thank you

You may also like

11 comments

alphonso July 16, 2020 - 4:56 pm

First i want to say thank you for the tutorial it was straight forward an fairly simple. When i use your link with images the code seems to work, however when i try to change the src location the images does not appear. Also i have jpg and png photos in this location how can i use both.

Reply
Ajay Malhotra July 16, 2020 - 5:16 pm

can you paste here the path that you are using for image src?

Reply
therichpost September 15, 2022 - 4:25 am

I updated the code and now please try

Reply
Anjali Panchal May 18, 2021 - 6:59 am

Hello this is nice tutorial but i can’t able to open light box so can you help me?

Reply
Ajay Malhotra May 18, 2021 - 7:06 am

Can you please tell me the issue?

Reply
therichpost September 15, 2022 - 4:25 am

Please use this latest and updated:
https://therichpost.com/angular-9-angular-ngx-lightbox/

Reply
Pravin G May 31, 2021 - 11:37 am

Hi Sir, lightbox is not working for Videos. Can you please help me what may be issue.

Reply
Ajay Malhotra May 31, 2021 - 11:46 am

Okay no issue and I will update this post for Angular version 12.

Reply
rajesh June 8, 2021 - 11:02 am

how to control zoomin & zoomout

Reply
Ajay Malhotra June 8, 2021 - 11:05 am

I will check and update you.

Reply
therichpost September 15, 2022 - 4:24 am

Please use this latest and updated:
https://therichpost.com/angular-9-angular-ngx-lightbox/

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.