【发布时间】:2017-10-04 16:16:31
【问题描述】:
我已经用 Angular 实现了一个简单的聊天应用程序。我正在使用来自@stomp/ng2-stompjs 的 stomp 套接字。我正在使用*ngFor 来显示所有消息。
<p *ngFor="let item of messages" style="padding: 5px; font-size: 18px">
<span style="color:darkturquoise">{{item.author}}</span>: {{item.message}}
</p>
但是当messages 变量更新时(我可以看到console.log),angular 不会重新渲染*ngFor。
this.subscription = this.messagesObs.subscribe((message: Frame) => {
this.messages = <ResponseMessage[]>JSON.parse(message.body).slice();
console.log(this.messages);
});
我试过this.changeDetector.detectChanges();,但没有帮助。
我知道这是框架的默认行为,但是有什么好的方法可以让*ngFor 在每次订阅后呈现?
更新:
我做了一个plunker 的例子。示例将不起作用,因为我的后端位于 localhost 上。
【问题讨论】:
标签: angular typescript