To create a shared favorite and disfavorite component for each product in Angular that uses images instead of buttons, you can follow these steps:
-
Create a new Angular component using the following command:
ng generate component ProductFavorite
-
Edit the HTML file of this component to include the favorite and disfavorite images:
<div class="favorite-wrapper">
<img src="../assets/favorite.png" class="favorite" *ngIf="!isFavorite" (click)="favorite()">
<img src="../assets/favorite-filled.png" class="favorite" *ngIf="isFavorite" (click)="disfavorite()">
</div>
- Create an input and output in this component to receive the current favorite status and emit the new favorite status:
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-product-favorite',
templateUrl: './product-favorite.component.html',
styleUrls: ['./product-favorite.component.css']
})
export class ProductFavoriteComponent {
@Input() isFavorite: boolean;
@Output() toggleFavorite = new EventEmitter<boolean>();
favorite() {
this.toggleFavorite.emit(true);
}
disfavorite() {
this.toggleFavorite.emit(false);
}
}
-
Repeat the above steps to create a new Angular component named ProductDisfavorite for the disfavorite image.
-
Create a new Angular component that will use these shared favorite and disfavorite components. For example, let's create a ProductListComponent component:
ng generate component ProductList
-
Edit the HTML file of this component to include the product list and the ProductFavorite and ProductDisfavorite components for each product:
<div class="row">
<ng-container *ngFor="let product of products">
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" [src]="product.imageUrl" alt="{{ product.name }}" />
<div class="card-body">
<h4 class="card-title">{{ product.name }}</h4>
<p class="card-text">{{ product.description }}</p>
</div>
<div class="card-footer">
<div class="row">
<div class="col">
<app-product-favorite [isFavorite]="product.isFavorite" (toggleFavorite)="toggleFavorite(product)"></app-product-favorite>
</div>
<div class="col">
<app-product-disfavorite [isDisfavorite]="product.isDisfavorite" (toggleDisfavorite)="toggleDisfavorite(product)"></app-product-disfavorite>
</div>
</div>
</div>
</div>
</div>
</ng-container>
</div>
- Edit the TypeScript file of this component to include the logic for toggling the favorite and disfavorite status of each product:
import { Component } from '@angular/core';
import { ProductService } from '../product.service';
@Component({
selector: 'app-product-list',
templateUrl: './product-list.component.html',
styleUrls: ['./product-list.component.css']
})
export class ProductListComponent {
products: any[] = this.productService.getProducts();
constructor(private productService: ProductService) {}
toggleFavorite(product: any) {
product.isFavorite = !product.isFavorite;
if (product.isFavorite) {
product.isDisfavorite = false;
}
this.productService.updateProduct(product);
}
toggleDisfavorite(product: any) {
product.isDisfavorite = !product.isDisfavorite;
if (product.isDisfavorite) {
product.isFavorite = false;
}
this.productService.updateProduct(product);
}
}
- Lastly, don't forget to include these new components in the
AppModule file:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ProductListComponent } from './product-list/product-list.component';
import { ProductFavoriteComponent } from './product-favorite/product-favorite.component';
import { ProductDisfavoriteComponent } from './product-disfavorite/product-disfavorite.component';
import { ProductService } from './product.service';
@NgModule({
declarations: [
AppComponent,
ProductListComponent,
ProductFavoriteComponent,
ProductDisfavoriteComponent
],
imports: [
BrowserModule
],
providers: [
ProductService
],
bootstrap: [AppComponent]
})
export class AppModule { }
That's it! You can now use the ProductListComponent component to display a list of products and allow users to favorite or disfavorite each product by clicking on the corresponding image.