Introduction

Today, we will create a simple demo application to display the images and the preview of those images.
Create Image Gallery Using Angular 7
Prerequisites
  • Knowledge of Angular 2 or higher
  • Basic knowledge of TypeScript
  • Visual Studio Code
  • Node and NPM installed

Technologies we will use to build this demo

  • Angular 7
  • Bootstrap
  • HTML and CSS
Step 1
Create an Angular project by using the following command.
ng new Imageedemo
Create Image Gallery Using Angular 7
Step 2
Open this project in Visual Studio Code.
Create Image Gallery Using Angular 7
Expand the src folder and right-click on the Assets folder. Add a new folder under it and rename that to 'Images'.
Create Image Gallery Using Angular 7
Now, open the Images folder and add some demo images to this folder.
Create Image Gallery Using Angular 7
Step 3
Now, open the integrated terminal by pressing Ctrl + ~.

Let's create a service to retrieve the images with the name 'image', by using the following command.

ng g service image
Now, open the image.service.ts file and add the following code.
  1. import { Injectable } from '@angular/core'
  2. @Injectable()
  3. export class ImageService {
  4. allImages = [];
  5. getImages() {
  6. return this.allImages = Imagesdelatils.slice(0);
  7. }
  8. getImage(id: number) {
  9. return Imagesdelatils.slice(0).find(Images => Images.id == id)
  10. }
  11. }
  12. const Imagesdelatils = [
  13. { "id": 1, "brand": "Apple", "url": "assets/Images/Macbook1.jpg" },
  14. { "id": 2, "brand": "Apple", "url": "assets/Images/MacBook.jpg" },
  15. { "id": 3, "brand": "Apple", "url": "assets/Images/laptop3.jpg" },
  16. { "id": 4, "brand": "Apple", "url": "assets/Images/laptop4.jpg" },
  17. { "id": 5, "brand": "hp", "url": "assets/Images/hp1.jpg" },
  18. { "id": 6, "brand": "hp", "url": "assets/Images/hp2.jpg" },
  19. { "id": 7, "brand": "hp", "url": "assets/Images/hp3.jpg" },
  20. { "id": 8, "brand": "hp", "url": "assets/Images/hp4.jpg" },
  21. { "id": 9, "brand": "Lenovo", "url": "assets/Images/laptop5.jpg" },
  22. { "id": 10, "brand": "Lenovo", "url": "assets/Images/laptop7.jpg" },
  23. { "id": 11, "brand": "Lenovo", "url": "assets/Images/laptop8.jpg" },
  24. { "id": 12, "brand": "Lenovo", "url": "assets/Images/laptop9.jpg" },
  25. { "id": 13, "brand": "Lenovo", "url": "assets/Images/laptop11.jpg" },
  26. { "id": 14, "brand": "asus", "url": "assets/Images/laptop13.jpg" },
  27. { "id": 15, "brand": "asus", "url": "assets/Images/laptop14.jpg" },
  28. { "id": 16, "brand": "asus", "url": "assets/Images/laptop15.jpg" },
  29. { "id": 17, "brand": "asus", "url": "assets/Images/laptop16.jpg" },
  30. { "id": 18, "brand": "asus", "url": "assets/Images/laptop17.jpg" },
  31. { "id": 19, "brand": "asus", "url": "assets/Images/laptop18.jpg" },
  32. { "id": 20, "brand": "asus", "url": "assets/Images/laptop20.jpg" },
  33. ]

Step 4

Now, create two components for displaying all imageslist and showing image preview, respectively. To create the components, open terminal and use the following commands.

ng g c ImageGallery
ng g c ImageDetails

Step 5
Now, open Image-Gallery.component.html and add the following HTML.
  1. <div>
  2. <div class="row">
  3. <div class="col-sm-12 btn btn-primary">
  4. Image Gallery
  5. </div>
  6. </div>
  7. <div class="row" style="margin-top:10px;margin-bottom: 10px;">
  8. <div class="col-sm-1">
  9. </div>
  10. <div class="col-sm-2">
  11. <button class="btn btn-success" (click)="filterBy='all'">All</button>
  12. </div>
  13. <div class="col-sm-2">
  14. <button class="btn btn-success" (click)="filterBy='Apple'">Apple</button>
  15. </div>
  16. <div class="col-sm-2">
  17. <button class="btn btn-success" (click)="filterBy='hp'">Hp</button>
  18. </div>
  19. <div class="col-sm-2">
  20. <button class="btn btn-success" (click)="filterBy='Lenovo'">Lenovo</button>
  21. </div>
  22. <div class="col-sm-2">
  23. <button class="btn btn-success" (click)="filterBy='asus'">Asus</button>
  24. </div>
  25. <div class="col-sm-1">
  26. </div>
  27. </div>
  28. <div class="row">
  29. <ul>
  30. <li *ngFor="let img of (allImages | filterimages:filterBy)">
  31. <a [routerLink]="['/image', img.id]">
  32. <img src="{{img.url}}" class="img" width="240" height="170">
  33. </a>
  34. </li>
  35. </ul>
  36. </div>
  37. </div>

Now, open ImageGallery.component.css and add the following lines.

  1. .img{
  2. margin:6px 6px;
  3. border: 4px solid #eee;
  4. box-shadow:rgb(224, 199, 199) 1px 1px 8px 1px;
  5. cursor: pointer
  6. }
  7. .modal-content {
  8. width: 1000px !important;
  9. }
  10. ul { padding:0; width:1200px; margin:20px auto;margin-left: 80px}
  11. li { display:inline;}
Now, open Image-Gallery.component.ts and add the following lines.
  1. import { Component, OnInit, Input, OnChanges } from '@angular/core';
  2. import { ImageService } from './image.service';
  3. @Component({
  4. selector: 'app-imagegallery',
  5. templateUrl: './image-gallery.component.html',
  6. styleUrls: ['./image-gallery.component.css']
  7. })
  8. export class GalleryComponent implements OnChanges {
  9. images:any[];
  10. filterBy?: string = 'all'
  11. allImages:any[] = [];
  12. constructor(private imageService: ImageService) {
  13. this.allImages = this.imageService.getImages();
  14. }
  15. ngOnChanges() {
  16. this.allImages = this.imageService.getImages();
  17. }
  18. }
Step 6
Now, open Image-Details.component.html and add the following HTML.
  1. <div class="row">
  2. <div class=col-md-12>
  3. <div [ngStyle]="{'background-image':'url('+ image.url +')'}" class="img-container">
  4. </div>
  5. </div>
  6. </div>

Now, open Image-Details.component.css and add the following lines.

  1. .img-container{
  2. margin:13px;
  3. box-shadow: rgb(211, 31, 31) 1px 2px 8px 1px;
  4. min-height: 600px;
  5. min-width: 800px;
  6. background-position: center;
  7. background-repeat: no-repeat;
  8. align-content: center;
  9. }
Now, open Image-Details.component.ts and add the following lines.
  1. import { Component } from '@angular/core';
  2. import { ImageService } from './image.service';
  3. import { ActivatedRoute } from '@angular/router'
  4. @Component({
  5. templateUrl: './image-detail.component.html',
  6. styleUrls: ['./image-detail.component.css']
  7. })
  8. export class ImageDetailComponent {
  9. image:any
  10. constructor(private imageService: ImageService,
  11. private route: ActivatedRoute) { }
  12. ngOnInit(){
  13. thisthis.image = this.imageService.getImage(
  14. this.route.snapshot.params['id']
  15. )
  16. }
  17. }
Step 7
Now, create a custom pipe using the following command to filter the data.
ng g pipe filterimages
Now, open the filterimages.pipe.ts file and add the following lines.
  1. import { Pipe, PipeTransform } from '@angular/core';
  2. @Pipe({
  3. name: 'filterimages'
  4. })
  5. export class FilterimagesPipe implements PipeTransform {
  6. transform(items: any[], laptop: string): any {
  7. if(laptop === 'all'){ return items } else
  8. return items.filter(item =>{
  9. return item.brand === laptop;
  10. });
  11. }
  12. }
Step 8
Open app.module.ts file and add the following code.
  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule } from '@angular/core';
  3. import { FormsModule } from '@angular/forms';
  4. import { RouterModule } from '@angular/router';
  5. import { ImageService } from './image.service';
  6. import { AppComponent } from './app.component';
  7. import { GalleryComponent } from './image-gallery.component';
  8. import { ImageDetailComponent } from './image-detail.component';
  9. import { appRoutes } from '../routes';
  10. import { FilterimagesPipe } from "./filterimages.pipe"
  11. @NgModule({
  12. declarations: [
  13. AppComponent,
  14. ImageGalleryComponent,
  15. ImageDetailComponent,
  16. FilterimagesPipe
  17. ],
  18. imports: [
  19. BrowserModule,
  20. FormsModule,
  21. RouterModule.forRoot(appRoutes)
  22. ],
  23. providers: [ImageService, FilterimagesPipe],
  24. bootstrap: [AppComponent]
  25. })
  26. export class AppModule { }
Step 9
Open app.component.html file and add this line of code.
  1. <div>
  2. <router-outlet></router-outlet>
  3. </div>
Step 10
Now, run the project and check the result.
Create Image Gallery Using Angular 7
Now, click on any button and check.
Create Image Gallery Using Angular 7
Click on any image and check.
Create Image Gallery Using Angular 7

Summary

In this article, we learned how we can create an image gallery using Angular and CSS.