简体   繁体   English

动态创建的组件的角度列表

[英]Angular list of components dynamically created

I have the following scenario, and I am going to use Facebook as an example: 我有以下情形,我将以Facebook为例:

If you look at the news/status feed within Facebook, if I was to implement something similar in Angular, would each status combined with comments and likes, be its own component, or implement it within a *ngFor ? 如果您查看Facebook内的新闻/状态供稿,如果我要在Angular中实现类似的功能,则将每个状态与评论和喜欢相结合,是其自身的组件,还是在*ngFor

Let's use some code as an example. 让我们以一些代码为例。

Example 1: not a component: 示例1:不是组件:

<ul *ngFor="let status of statusses">
    <li>{{status.description}}</li>
    <!-- some more Div tags -->
    <input id="comment" />
    <button id="comment"/>
</ul>

This example will have one component, for instance, Statuses . 此示例将包含一个组件,例如Statuses This component will have an array of all statuses , and logic for clicking on comment button etc. 该组件将包含所有statuses的数组,以及单击注释按钮等的逻辑。

Example 2: List of components 示例2:组件列表

<!-- List of single status components -->
<ng-template></ng-template>

In this example, the Statuses component will only have logic for retrieving statuses, and the component displaying the status, will be responsible for logic of commenting etc. 在此示例中, Statuses组件将仅具有用于检索状态的逻辑,而显示状态的组件将负责注释等的逻辑。

Is example two possible within Angular 4+ ? 在Angular 4+中可能有第二个示例吗?

Is this breaking down in to a too granular approach, or is this expected? 是分解为过于细化的方法,还是预期的?

Actually, method 2 is the recommended way. 实际上,方法2是推荐的方法。

If you look at the Tour of Heroes example you will see that there is heroes and a hero-detail component. 如果查看“英雄旅”示例,您将看到其中有heroeshero-detail部分。

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

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