简体   繁体   English

将不同的数据传递到Angular中的组件

[英]Passing different data to component in Angular

I'm trying to create a basic app in angular and I got the following issue. 我正在尝试用angular创建一个基本的应用程序,但遇到了以下问题。 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. 对于配方2->配方2数据..现在,我正在显示所有其他配方的配方1数据。

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">&times;</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! 提前致谢!

The problem is, all your modals will have the exact same static id of #recipeModal . 问题是,您所有的模态将具有#recipeModal完全相同的静态ID。

You can set dynamic IDs as well as the targets by utilizing the index of the ngFor like this: 您可以使用ngFor的索引来设置动态ID和目标,如下所示:

*ngFor="let recipe of recipes; let i = index"

Then setting the id attribute per recipe dynamically like this: 然后像这样动态设置每个配方的id属性:

<div class="modal fade" [attr.id]="'recipeModal' + i" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">

This part [attr.id]="'recipeModal' + i" will result in a unique id per recipe. 这部分[attr.id]="'recipeModal' + i"将导致每个配方具有唯一的ID。

Finally for your button, you would bind to the data-target dynamically as well : 最后,对于您的按钮,您还将动态绑定到数据目标:

<button type="button" class="btn btn-outline-primary" data-toggle="modal" [attr.data-target]="'recipeModal' + i">

EDIT 编辑

Here is the way of passing down the input 这是传递输入的方法

  • Add a new input to the component : @Input() id: number; 向组件添加新输入: @Input() id: number;
  • Pass the index as input for each component: <app-recipe-item *ngFor="let recipeItem of recipes; let i = index" [id]="i" [recipe]="recipeItem"></app-recipe-item> 将索引作为每个组件的输入传递: <app-recipe-item *ngFor="let recipeItem of recipes; let i = index" [id]="i" [recipe]="recipeItem"></app-recipe-item>
  • Edit the passed index to reference the ts: [attr.data-target]="'recipeModal' + {{id}}" and for the button as well : [attr.data-target]="'recipeModal' + {{id}}" 编辑传递的索引以引用ts: [attr.data-target]="'recipeModal' + {{id}}"以及按钮: [attr.data-target]="'recipeModal' + {{id}}"

I see the thing like this: 我看到这样的事情:

In your list component where you need to display all yours recipes in your .ts you have your let recipes = Recipe[] then in your .html you'll have a kind of <div *ngFor="let recipe of recipes" class="card"> to loop trough your list and display the recipe and then when you click on your button you've on it the event (click)="detailsRecipe(recipe)" 在您需要在.ts中显示所有食谱的列表组件中,您可以使用let recipes = Recipe[]然后在.html中,您可以使用<div *ngFor="let recipe of recipes" class="card">循环显示您的列表并显示配方,然后在单击按钮时单击该事件(click)="detailsRecipe(recipe)"

This method detailsRecipe(recipe) could open into a modale the detailsRecipeComponent and maybe in this details component, calling a service to get more informations about the recipe and that's it. 此方法detailsRecipe(recipe)可以将detailsRecipeComponent以及可能在此details组件中打开的方式打开,以调用服务以获取有关食谱的更多信息,仅此而已。 Of course it's a draft that I present you but this is the general idea. 当然,这是我向您介绍的草稿,但这是总体思路。 Now you need to investigate how to open a modal with component it's depend of documentation about which UI Components you choosed. 现在,您需要研究如何使用组件打开模式,这取决于您选择了哪个UI组件的文档。

I hope it could help you. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM