简体   繁体   English

如何在Angular中以NgFor为基础构建内部NgFor

[英]How to base an inner NgFor on the outter NgFor in Angular

I'm trying to display a list of conversations in my Angular app, & then display the messages contained within those various conversations underneath each Conversation Title . 我正在尝试在我的Angular应用程序中显示对话列表,然后在每个对话标题下显示各种对话中包含的消息。

Here is my latest code - 这是我最新的代码 -

HTML: HTML:

<div
   class="card"
   style="width: 18rem;"
   *ngFor="let conversation of myConversations"
>
<div class="card-body">
    <h5 class="card-title">{{ conversation.conversationTitle }}</h5>
    <h6 class="card-subtitle mb-2 text-muted">
       Conversation ID: {{ conversation.conversationId }}
    </h6>
    <p *ngFor="let message of myConversationMessages" class="card-text">
       {{ message.messageText }}
    </p>
</div>

TS: TS:

myConversations: IConversation[] = [];
myConversationMessage: IConversationMessages = {
conversationId: 0,
messageId: 0,
messageText: ''
}; 
myConversationMessages: IConversationMessages[] = [];
constructor(private conversationService: ConversationService) {}

ngOnInit() {
this.conversationService.getConversations().subscribe(conversations => {
  this.myConversations = conversations;
  this.displayMessages();
});
}

displayMessages() {
for (let i of this.myConversations) {
  for (let j of i.messages) {
    this.myConversationMessages.push({
        conversationId: i.conversationId,
        messageId: j.messageId,
        messageText: j.messageText
    });
  }
}
console.log(this.myConversationMessages);
}

This is what I'm currently able to display at the moment: 这是我目前能够显示的内容:

在此输入图像描述

Each conversation has it's own card, but the messages are being repeated for all conversations, regardless of which conversation they're connected to. 每个对话都有自己的卡片,但是无论他们连接到哪个对话,所有对话都会重复这些消息。

I think I need to make some change to the inner ngFor , but I'm not exactly sure which change to make. 我想我需要对内部ngFor进行一些更改,但我不确定要做出哪些更改。 Any ideas on what changes are required? 有关需要进行哪些更改的任何想法? Thanks! 谢谢!

Also, here is the associated JSON: 此外,这是相关的JSON:

[
  {
    "conversationId": 1,
    "conversationTitle": "My first convo",
    "messages": [
      {
        "messageId": 1,
        "messageText": "Hi"
      },
      {
        "messageId": 2,
        "messageText": "Hello"
      }
    ]
  },
  {
    "conversationId": 2,
    "conversationTitle": "My second convo",
    "messages": [
      {
        "messageId": 1,
        "messageText": "test"
      },
      {
        "messageId": 2,
        "messageText": "testing"
      }
    ]
  }
]

Based of the JSON you have provided you should be able to use an *ngfor , inside the *ngfor to read the messages. 根据您提供的JSON,您应该能够在*ngfor使用*ngfor来阅读消息。 I have stripped away some of the elements but the following should give you the result you need. 我已经删除了一些元素,但以下内容应该可以为您提供所需的结果。 Based of the JSON in the question. 基于问题中的JSON。

Solution

<div class="card" style="width: 18rem;" *ngFor="let conversation of myConversations">
    <div class="card-body">
        <h5 class="card-title">
            {{ conversation.conversationTitle }}
        </h5>

        <h6 class="card-subtitle mb-2 text-muted"> Conversation ID:
            {{ conversation.conversationId }}
        </h6>

        <div *ngFor="let message of conversation.messages" class="card-text">
            <span>
                {{ message.messageText }}
            </span>
        </div>
    </div>
</div>

If the JSON is the initial myConversations then you will not need to do anything more with that data as it is already enough to work with. 如果JSON是最初的myConversations那么您将不需要对该数据执行任何其他操作,因为它已经足够使用了。

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

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