简体   繁体   English

当我点击 Angular 上的按钮时,如何获得自动刷新?

[英]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.

相关问题 尽管自动刷新页面,但单击时仍隐藏跨度 - hide span when i click on it despite the automatic page refresh 单击按钮时页面会奇怪地刷新 - The page will strangely refresh when I click the button 当我单击按钮时,为什么我的角度应用程序会刷新数据源中的所有行? - Why does my angular app refresh all rows from the data source when I click a button? Angular 2路由器在刷新之前没有加载,直到我点击按钮 - Angular 2 router not loading on refresh until I click button 单击该按钮后,如何获得一个可播放音频的img按钮? - How can I get an img button that plays audio when you click the button? angular中ngx-quill的编辑器为空时,如何禁用点击按钮? - How can I disable the click button when the editor of ngx-quill is empty in angular? 当我单击按钮时,如何显示我的Facebook对话框? - How can I get my Facebook dialog to show up when I click the button? 当我在 MUI 中单击他们的按钮时,如何获得价值元素? - How can I get element of value, when I click their button in MUI? 当我单击按钮并将其存储在会话变量中时,如何获取链接 - How can i get link when i click on button and stored in session variable 如果单击并按住一个按钮,如何使用angular一次又一次触发鼠标单击按钮? - how can I fire the mouse click button again and again using angular if I click and hold a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM