Skip to main content

Command Palette

Search for a command to run...

Activity #31: Angular Ecommerce Product List

Published
2 min read
Activity #31: Angular Ecommerce Product List

product.service.ts

import { Injectable } from '@angular/core';
import { ProductModel } from '../components/product';

@Injectable({
  providedIn: 'root'
})
export class ProductService {
  private products: ProductModel[] = [
    {
      id: 1,
      name: "Cosori Air Fryer",
      price:  10800,
      imageUrl: "https://i.pinimg.com/736x/28/8a/74/288a74ca157de712c14b0ed6ad22e56b.jpg"
    },
    {
      id: 2,
      name: "JisuLife Handheld Fan",
      price:  999,
      imageUrl: "https://i.pinimg.com/736x/11/43/2d/11432d325c16213dc5737f104bfa5073.jpg"
    },
    {
      id: 3,
      name: "Maybelline Fit Me Foundation",
      price:  371,
      imageUrl: "https://i.pinimg.com/736x/55/5e/a4/555ea40b83320d8e671b6e031ea8a1c4.jpg"
    },
    {
      id: 4,
      name: "Maybelline Sky High Mascara",
      price:  499,
      imageUrl: "https://i.pinimg.com/736x/c1/f5/ba/c1f5baabdbbc2925a295db42ee236c9b.jpg"
    },
    {
      id: 5,
      name: "Banila Co Clean it Zero Cleansing Balm",
      price:  649,
      imageUrl: "https://i.pinimg.com/736x/b6/70/a9/b670a9609f99b168b2cf3bf899f26a03.jpg"
    },
    {
      id: 6,
      name: "SKIN1004 Madagascar Centella Sun Serum",
      price:  608,
      imageUrl: "https://i.pinimg.com/736x/be/fc/a7/befca7273a03c9c3ff48b6c1fdf86de9.jpg"
    },
    {
      id: 7,
      name: "Short Cami Dress",
      price:  500,
      imageUrl: "https://i.pinimg.com/736x/43/da/ba/43daba9ec9ef33748efb0f1e6323af8b.jpg"
    },
    {
      id: 8,
      name: "Denim Maxi Skirt",
      price:  500,
      imageUrl: "https://i.pinimg.com/736x/2d/02/da/2d02da40a2ec3eff9ec81fc8e807f236.jpg"
    },
    {
      id: 9,
      name: "Converse Run Star Hike Canvas Platform",
      price:  5290,
      imageUrl: "https://i.pinimg.com/736x/00/8c/a1/008ca1d7d9148f59ef9cc4de1606df9f.jpg"
    },
    {
      id: 10,
      name: "New Balance 530",
      price:  5873,
      imageUrl: "https://i.pinimg.com/736x/18/62/35/186235eed95b4ccbc97f3620e6c2cc9d.jpg"
    }
  ];

  getProduct(): ProductModel[] {
    return this.products;
  }
}

product.model.ts

export interface ProductModel{
  id: number;
  name: string;
  price:  number;
  imageUrl: string;
  }

product.component.ts

import { ProductService } from './../../services/product.service';
import { Component, OnInit } from '@angular/core';
import { ProductModel } from '../product';

@Component({
  selector: 'app-product',
  templateUrl: './product.component.html',
  styleUrl: './product.component.css'
})
export class ProductComponent implements OnInit{
  productList: ProductModel[] = [];

  constructor(private productService: ProductService) {}

  ngOnInit(): void {
    this.productList = this.productService.getProduct();
  }
}

product.component.html

<!DOCTYPE html>
<html>
<head>
  <title>Angular Eccomerce Product List</title>
</head>
<body>
  <div class="product-grid">
  <div *ngFor="let product of productList" class="product-grid__item">
    <img
      class="product-grid__image"
      [src]="product.imageUrl"
    />
    <h2 class="product-grid__name">{{ product.name }}</h2>
    <p class="product-grid__price">₱{{ product.price }}</p>
    <button class="product-grid__button">Add to Cart</button>
  </div>
  </div>
</body>
</html>

product.component.css

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: Arial, sans-serif;
  background-color: #f4f4f4;
  padding: 1rem;
}

.product-grid {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(2, 1fr);
}

@media (min-width: 768px) {
  .product-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 1024px) {
  .product-grid {
    grid-template-columns: repeat(5, 1fr);
  }
}

.product-grid__item {
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 8px;
  text-align: center;
  padding: 1rem;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.product-grid__image {
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 4px;
  margin-bottom: 0.5rem;
}

.product-grid__name {
  font-size: 1rem;
  font-weight: bold;
  margin-bottom: 0.5rem;
}

.product-grid__price {
  color: #555;
  margin-bottom: 1rem;
}

.product-grid__button {
  background-color: #333;
  color: #fff;
  border: none;
  padding: 0.5rem;
  border-radius: 5px;
  cursor: pointer;
}

.product-grid__button:hover {
  background-color: #3498db;
}

DESKTOP

TABLET

MOBILE

FIREBASE Hosting URL:

https://angular-ecommerce-product-list.web.app

GITHUB:

https://github.com/jennyfaye/AngularEcommerceProductList.git