Data only displayed after refresh

后端 未结 2 1999
梦毁少年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:30

    You have to call your getComments method in the callback of you submit comments. Your code only has a "get" call in ngOnInit method of the component. So when you refresh your view, the ngOnInit executes again and thus getComments is called.

    You have to make a get call in the callback of your submit comments method.

    EDIT#1:

    addReview(author, description) {
        this.moviesService.addReview(author, description).subscribe(success => {
            this.flashMessages.show('You are data we succesfully submitted', { cssClass: 'alert-success', timeout: 3000 });
            // get the id
            this.moviesService.getComments(id).subscribe(comments => {
                console.log(comments);
                this.comments = comments;
            });
        }, error => {
            this.flashMessages.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 });
        });
    }
    

提交回复
热议问题