【问题标题】:Angular - How to trigger a *ngFor directive again after pushing some datas in an array?Angular - 在数组中推送一些数据后如何再次触发 *ngFor 指令?
【发布时间】:2018-07-31 17:46:06
【问题描述】:

我在请求后在我的数组中添加一个对象。一切顺利,但我希望在收到响应的同时显示新数据。当我收到响应时,我将新对象推送到我的数组中,用于在我的视图中显示消息。

我尝试使用ChangeDetectorRef(detectChanges() 方法)和NgZone(run() 方法)但没有成功。基本上我希望我的视图再次出现在 ngFor 上,以便在发送时显示最后一条消息。

我在互联网上搜索,但找不到任何关于 Angular 的答案,除了一个不起作用的线程。

这是我想要触发消息呈现的代码部分:

this.chatRequestService.send(this.navParams.get('id_chat'), nmMessage, this.token.value).subscribe(
            (result) => {
              this.messages.push(result);
            },
            (error) => {
              console.log(error);
            }
          );

这是我在模板中的 ngFor 指令:

<ng-container *ngFor="let message of messages[0]">
        <ion-row>
            <ion-col col-1 class="round-right"><ion-avatar item-start><img src="assets/imgs/BK.png" alt="Burger King" title="Burger King"></ion-avatar></ion-col> <!-- remplacer par {{message[0]?.sender.profileImageUrl}} -->
            <ion-col col-9>
                <div *ngIf="currentUser == message?.sender.id else elseBlock1" class="talk-bubble round-left me">
                    <div class="talktext">
                        <h5>{{message?.sender.username}}</h5>
                        <p>{{message?.content}}</p>
                    </div>
                </div>
                <ng-template #elseBlock1>
                    <div class="talk-bubble round-left">
                        <div class="talktext">
                            <h5>{{message?.sender.username}}</h5>
                            <p>{{message?.content}}</p>
                        </div>
                    </div>
                </ng-template>
            </ion-col>
        </ion-row>
    </ng-container>

提前感谢愿意花时间阅读/回答的任何人。

【问题讨论】:

  • 您可能正在寻找trackBy stackoverflow.com/questions/42108217/…
  • 我真的不明白我将如何使用它?
  • 请向我们展示模板和代码(相关部分)。
  • 发布更新,ty :)
  • 您还应该显示数据结构。我不清楚你为什么使用*ngFor="let message of messages[0]"。为什么是[0]?由于您在代码中messages 的末尾添加了一个新项目,您不应该确保显示最后一个项目,而不是第一个项目吗?或者可能都是messages

标签: angular templates ngfor


【解决方案1】:

您可以使用trackBy*ngFor,在数组数据更改(添加/删除项目)后自动更新视图。有关详细说明,请查看 post

以同样的方式,您可以实现一些自定义函数来跟踪列表中的项目。因此,您必须在 messages[0] 数组中选择唯一标识符。例如:sender.username 并从函数中返回此值。

identify(index, message) {
     return message.sender.username; 
}

html

<ng-container *ngFor="let message of messages[0]; trackBy:identify">
       ...................
</ng-container>

【讨论】:

  • 谢谢,我做了identify函数来返回message.id,因为这是每条消息的唯一标识符,但测试时没有改变。
  • 使用您的示例逐个字符,它也不起作用。但是,如果我将 console.log 放在 identify 函数中,它会在每个 DOM 事件中触发。必须是正确的使用方法,但我仍然缺少一些东西。您对此的帮助将非常方便。 :)
  • 你能告诉我,messages 打印什么而不是messages[0]
  • 我的 for 循环正在工作,当我访问页面时,以前的消息显示良好,只有当我在组件的 Array 属性中推送新发送的消息时,新消息才不是添加到消息列表中。
  • 如果我让消息的消息,则会引发错误。我完全确定我的数据结构。
【解决方案2】:

您可以尝试使用 slice() 复制具有新实例的相同数组。这是示例代码

this.customerArray = this.customerArray.slice();

【讨论】:

  • 它不会解决我的问题,因为重新创建一个新实例不会再次触发 ngFor 指令
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多