Data only displayed after refresh

后端 未结 2 1997
梦毁少年i
梦毁少年i 2021-01-07 15:01

I am creating comments section in my app, when a user enter a comments and submit, comment should imeaditely be displayed in front end, unfortunately now comments are only v

2条回答
  •  伪装坚强ぢ
    2021-01-07 15:12

    @Kaczkapojebana. When we subscribe to a "get", DON'T mean that a change in the dbs was displayed in the view. (only say that when the asyncronous called is completed, the data is showed). I usually refered to "get" subscription as "subscription of only one use".

    You must add manually to this.comments the new data. where? IN subscribe function:

    addReview(author, description) { 
        this.moviesService.addReview(author, description).subscribe(success => {  
          /***add manually to this.comments***********/
          this.comments.push({author:author,descripcion:description});
    
          this.flashMessages.show('You are data we succesfully submitted', { cssClass: 'alert-success', timeout: 3000 });
        }, error => {
          this.flashMessages.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 });
        });
      }
    

    NOTE: You can also subscribe again to get all the data or that the addReview response all the data NOTE2: Equally, when you make an update, you must update the "this.comments" array manually

提交回复
热议问题