I want to check if an id already exists or not in local-storage, because I want to display specific button if it does. How can I achieve that?
here is my code
export class FoodPageComponent implements OnInit {
buttonShow = false;
food!: Food;
constructor(
activatedRoute: ActivatedRoute,
foodService: FoodService,
private cartService: CartService,
private router: Router
) {
activatedRoute.params.subscribe((params) => {
if(params.id)
this.food = foodService.getFoodById(params.id);
})
}
ngOnInit(): void {
}
addToCart() {
this.cartService.addToCart(this.food);
this.router.navigateByUrl('/cart-page');
}
}
Try this:
ngOnInit(): void {
let storageItems = localStorage.getItem('item');
if(storageItems) {
const items = JSON.Parse(items);
const requiredItem = items.filter((item)=> item.id == yourId);
if(requiredItem){
this.buttonShow = true;
}
}
}