Color image

Angular Ngx-Lightbox

lightbox2 implementation port to use with new Angular without the need for jQuery.

Version

1. Here is the command you need to run to install the lightbox module into your angular 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 {
  public _album: Array<any> = [];
  constructor(public _lightbox: Lightbox) {
    for (let i = 1; i <= 4; i++) {
      const src = 'demo/img/image' + i + '.jpg';
      const caption = 'Image ' + i + ' caption here';
      const thumb = 'demo/img/image' + i + '-thumb.jpg';
      const album = {
         src: src,
         caption: caption,
         thumb: thumb
      };
      this._album.push(album);
    }
  }
 
  open(index: number): void {
    // open lightbox
    this._lightbox.open(this._album, 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 _album; let i = index;">
  <img [src]="image.thumb" (click)="open(i)" />
</div>

Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *