I'm trying to create a basic app in angular and I got the following issue. I have a component where I list all the recipes I have and every recipe has different data. I added a button to every recipe which opens up a modal with the additional information to that specific object, at least thats the plan. Right now I'm always showing the same data, no matter which button I click. Can I please have a few tips on how to continue and always show the correct data? For recipe2 -> recipe2 data.. right now I'm showing recipe 1 data for all the other recipes.
Here is the component:
<div class="col my-3">
<div class="card">
<img [src]="recipe.imagePath" alt="{{ recipe.name }}" class="card-img-top">
<div class="card-body">
<h5 class="card-title">{{ recipe.name }}</h5>
<p class="card-text">{{ recipe.preview }}</p>
<div class="row">
<div class="col text-center">
<button type="button" class="btn btn-outline-primary" data-toggle="modal" data-target="#recipeModal">
Read More...
</button>
</div>
</div>
<div class="modal fade" id="recipeModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">{{ recipe.name }}</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>{{ recipe.description }}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-success">Add to List</button>
<button type="button" class="btn btn-outline-warning">Edit</button>
<button type="button" class="btn btn-outline-danger">Delete</button>
</div>
</div>
</div>
</div>
<!-- <app-recipe-detail></app-recipe-detail> -->
</div>
<div class="card-footer">
<small class="text-muted">Posted by: Random Name</small>
</div>
</div>
And the typescript file:
import { Component, OnInit, Input } from '@angular/core';
import { Recipe } from '../../recipe.model';
@Component({
selector: 'app-recipe-item',
templateUrl: './recipe-item.component.html',
styleUrls: ['./recipe-item.component.css']
})
export class RecipeItemComponent implements OnInit {
@Input() recipe: Recipe;
constructor() { }
ngOnInit() {
}
}
And here is how I display all recipes in a list component:
<div class="row">
<app-recipe-item *ngFor="let recipeItem of recipes" [recipe]="recipeItem"></app-recipe-item>
</div>
Thanks in advance!
#recipeModalthat you have declared in the button, I cannot find it anywhere else. You should instead take the recipe id value from your file in your button and pass the id to your modal. Try it with two separate buttons with two separate id.