Todo List Tutorial
Searchโ€ฆ
#0: Init photo gallery

Setup project

Let's start with creating new project - as you probably remember we can do it from terminal with command
1
ng new <app-name>
Copied!
Tip This tutorial presents scss styling so choose that option during project creation.
At the begining we want to just display photos in gallery. To create it we need one service (to manage our photos) and two components (photo for displaying separate image and gallery to display collection of photos).
Reminder: To create component we can use command
1
ng g c <component-name>
Copied!
and to create service
1
ng g s <service-name>
Copied!

Create Photo interface

Now, we have to define how our photo objects will look like - for each photo we need info about its id, url, descrption and optionally categoryID. So let's create an interface Photo by command:
1
ng g i photo
Copied!
Our Photo interface should look like this:
1
export interface Photo {
2
url: string;
3
description: string;
4
id: string;
5
categoryID?: string;
6
}
Copied!

Photo service

Photos service will be responsible for all actions connected with management of photos. It starts with one field: photos - an array with a bunch of example photos. So photos in Photos service has type Photo[].
First weโ€™re going to use it to display our gallery. Later, weโ€™ll gradually add Observables and interactvity, based on this array with photos.
We need some initial photos in our gallery so just copy-paste array from snippet below in your photo.service.ts (above the class body).
1
const initialPhotos: Photo[] = [
2
{
3
url: "https://66.media.tumblr.com/dff05f90167b5e50eab4df4f61a309aa/tumblr_o1ro152Q1m1rbkxlgo1_500.jpg",
4
description: "",
5
id: "6d3238a0-8e7b-4f58-b799-37ad6072097e",
6
},
7
{
8
url: "https://66.media.tumblr.com/be45669e5825a1db9c22c730c00eb5db/tumblr_o6ckp64vBe1vnm7bio1_500.jpg",
9
description: "",
10
id: "eec3632f-2364-47d5-8f87-504b98c8fc83",
11
},
12
{
13
url: "https://66.media.tumblr.com/9251ed46399400b08d15993800972c08/tumblr_pw9iqdF4Qr1tfqi0so1_500.jpg",
14
description: "",
15
id: "02cc506c-a6f4-47f7-856b-cceec9e54190",
16
},
17
{
18
url: "https://66.media.tumblr.com/24afdb14e85f6d48d482ae3a6af83c57/tumblr_pxx0abif9Q1qdsqp6o1_500.jpg",
19
description: "",
20
id: "279dd724-29c2-4b14-aedd-3c5f3f53ab9e",
21
},
22
{
23
url: "https://66.media.tumblr.com/00cc8515932da313bba8335cc203f8ce/tumblr_pxyqayFKUm1qdsqp6o1_500.jpg",
24
description: "",
25
id: "d5af369b-7723-4fc9-856c-e3c2b3cfb03b",
26
},
27
{
28
url: "https://66.media.tumblr.com/c9a8653bf0afb576e18e6f4b4c65288b/tumblr_py46p7VQNi1qdqlnso1_500.jpg",
29
description: "",
30
id: "28daea7d-d615-45a0-ac0b-b0d0bfc9a046",
31
},
32
{
33
url: "https://66.media.tumblr.com/fbf867c85348db723a08e1c29f6449fb/tumblr_pqkkz4dePA1wfvbkto1_500.jpg",
34
description: "",
35
id: "d7bf8d6c-9358-4a51-b5a8-5decf7ad33cc",
36
}
37
];
Copied!
and put it as initial value for photos property (inside the PhotoService class)
1
photos: Photo[] = initialPhotos;
Copied!
Thanks to that we have simple list of photos we can present in gallery component.

Gallery component

Gallery Component refers to the list of photos, so we have to add corresponding photosList property in gallery.component.ts file:
1
photosList: Photo[] = this.photosService.photos;
Copied!
Don't forget to inject the service in component's constructor like here:
1
constructor(private photosService: PhotosService) {}
Copied!
Now you can display our list using *ngFor in gallery.component.html file:
1
<div class="photos-list">
2
<div *ngFor="let photo of photosList" class="photo">
3
<app-photo [photo]="photo"></app-photo>
4
</div>
5
</div>
Copied!

Photo component

As you see thereโ€™s one Component left to implement: app-photo which displays an individual photo.
In the photo.component.ts declare one Input
1
@Input() photo: Photo;
Copied!
and use this data in HTML template
1
<img (click)="onPhotoClick(photo.id)" [src]="photo.url">
Copied!

Add components to app view

We created a lot of code but nothing is visible in the bropwser preview! We can easily fix it just by adding gallery component in app.component.html file
1
<div class="photo-gallery-app">
2
<app-gallery></app-gallery>
3
</div>
Copied!

Styling

To make our app beautiful add following code to gallery.component.scss
1
.photos-list {
2
display: flex;
3
flex-wrap: wrap;
4
margin-top: 3rem;
5
}
6
โ€‹
7
.photo {
8
width: 18vw;
9
height: 20vw;
10
display: flex;
11
flex-direction: column;
12
padding-bottom: 1rem;
13
margin-left: 1.7vw;
14
}
Copied!
Height property to :host and .photo-gallery-app in app.component.scss
1
:host, .photo-gallery-app {
2
height: 100%;
3
}
Copied!
and belowed snippet to photo.component.scss
1
:host, img {
2
width: 100%;
3
height: 100%;
4
overflow: hidden;
5
}
6
โ€‹
7
:host {
8
cursor: pointer;
9
โ€‹
10
&, img {
11
transition: all 0.2s ease-in-out;
12
}
13
โ€‹
14
img {
15
object-fit: cover;
16
}
17
โ€‹
18
&:hover {
19
opacity: 0.9;
20
filter: drop-shadow(9px 9px 10px #333) contrast(140%);
21
โ€‹
22
img {
23
transform: scale(1.05);
24
}
25
}
26
}
Copied!
Congrats! You've just prepared initial version of our gallery!
Last modified 2yr ago