I have two different paths. One is for products and the other is for products-cart. I want to use a shared ts file for both to store the product and cart information in an array. However, I am encountering an issue. I am unable to make any changes or transfers. Whenever I add something to the cart array, the data I retrieve from the product-cart remains empty. I was advised to resolve this using observables, but my attempts were unsuccessful. My goal is to have 3 products that can be added from localhost:4200/products and viewed in localhost:4200/products/product-cart even though these are separate routes. Note (URLs are for images)
data.service.ts
import { Injectable } from "@angular/core";
import { Product } from "./product";
@Injectable()
export class DataService{
productsData:Array<Product>=[
{name:'Tomato',total:10, url:'https://migrostvstr.blob.core.windows.net/migrostv/2021/08/domates-kg-c7462d-1650x1650-1.jpg'},
{name:'Potato',total:27, url:'https://migros-dali-storage-prod.global.ssl.fastly.net/sanalmarket/product/28303000/patlican-kemer-kg-2ac52c-1650x1650.jpg'},
{name:'Garlic',total:20, url:'https://migros-dali-storage-prod.global.ssl.fastly.net/sanalmarket/product/28293383/28293383-874ca9-1650x1650.jpg'}
];
productscard:Array<Product>=[];
constructor(){}
}
products.component.ts
import { Component, OnInit,Output } from '@angular/core';
import { EventEmitter } from '@angular/core';
import { DataService } from '../data.service';
import { Product } from '../product';
@Component({
selector: 'app-products',
templateUrl: './products.component.html',
styleUrls: ['./products.component.css']
})
export class ProductsComponent implements OnInit {
constructor(private data:DataService) { }
ngOnInit() {
console.log(this.data.productsData)
}
getItems(value){
}
}
product-cart.components.ts
import { Component, OnInit } from '@angular/core';
import { Input } from '@angular/core';
import { Product} from '../product';
import { DataService } from '../data.service';
@Component({
selector: 'app-product-cart',
templateUrl: './product-cart.component.html',
styleUrls: ['./product-cart.component.css']
})
export class ProductCartComponent implements OnInit {
constructor(private datacart:DataService) { }
ngOnInit (){
console.log(this.datacart.productscard)
}
}
products.component.html
<div class="products">
<div class="product-cards" *ngFor="let items of newData">
<img class="icons" src="{{items.url}}">
<div class="items-style">
<div>
Product name: {{items.name}}
</div>
<div>
Quantity: {{items.total}}
</div>
<a class="add-icon"><i class="fas fa-cart-plus fa-2x icon"(click)="getItems(items)" ></i></a>
</div>
</div>
</div>