[英]How can I get an automatic refresh when I click a button on Angular?
I'm building a chat application.我正在构建一个聊天应用程序。 All works fine;
一切正常; however, when I click send, the message isn't displayed until I manually reload the page.
但是,当我单击发送时,直到我手动重新加载页面后才会显示该消息。
I think I'm missing something after the subscribe.我想我在订阅后遗漏了一些东西。
onSubmit() {
this.chatService
.sendMessage(
'https://db_url/message.json',
{
message: this.chatForm.value.message,
username: this.authService.user.displayName,
}
)
.subscribe((data) => {
// ...
console.log(data);
});
this.chatForm.reset();
}
Could someone point out what I might be missing or doing wrong in my code?有人可以指出我的代码中可能遗漏或做错了什么吗? Thank you.
谢谢你。
You can try storing the subscribed data in a messages array like messages: Array<Message> = [];
您可以尝试将订阅的数据存储在消息数组中,例如
messages: Array<Message> = [];
and then subscribe to it on the onInit() method, and display the messages maybe with <div *ngFor="let message of messages | slice: ind:messages.length">
(or just display it on the console, whatever you want).然后在 onInit() 方法上订阅它,并可能使用
<div *ngFor="let message of messages | slice: ind:messages.length">
显示消息(或者只是在控制台上显示它,无论你想要什么). It may also be because of subscribing to the sendMessages function instead of the getMessages?也可能是因为订阅了sendMessages function 而不是getMessages? (without reading the rest of the code, I can't tell exactly)
(没有看代码的rest,我也说不清楚)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.