Reputation: 5556
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 visible after refresh,
Here is what I have :
Displaying comments
component.ts
ngOnInit() {
this.activeRouter.params.subscribe((params) => {
// tslint:disable-next-line:prefer-const
let id = params['id'];
this.moviesService.getComments(id)
.subscribe(comments => {
console.log(comments);
this.comments = comments;
});
});
}
service.ts
getComments(id: string): Observable<any> {
const url = `${apiUrl + this.commentsUrl}/${id}`;
return this.http.get(url, httpOptions).pipe(
map(this.extractData),
catchError(this.handleError));
}
HTML
<div *ngFor="let comment of comments" class="col-md-7">
<ul class="list-group">
<li class="list-group-item">Author: {{comment.author}}</li>
<li class="list-group-item">Comments: {{comment.description}}</li>
</ul>
<br>
</div>
**
Adding comments:
** service.ts
// Adds comments
addReview(author, description) {
const uri = 'http://localhost:8000/movies/comments';
const obj = {
author: author,
description: description
};
return this.http.post(uri, obj);
}
compo.ts
createForm() {
this.angForm = this.fb.group({
author: ['', Validators.required],
description: ['', Validators.required]
});
}
addReview(author, description) {
this.moviesService.addReview(author, description).subscribe(success => {
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 });
});
}
HTML
<div class="col-md-7 movie-form" >
<flash-messages></flash-messages>
<form [formGroup]="angForm" novalidate>
<div class="form-group">
<label class="col-md-4">Author</label>
<input type="text" class="form-control" name="author" formControlName="author" #author />
</div>
<div *ngIf="angForm.controls['author'].invalid && (angForm.controls['author'].dirty || angForm.controls['author'].touched)"
class="alert alert-danger">
<div *ngIf="angForm.controls['author'].errors.required">
Name is required.
</div>
</div>
<div class="form-group">
<label class="col-md-4">Description</label>
<textarea cols="10" rows="10" type="text" class="form-control" formControlName="description" #description>
</textarea>
</div>
<div *ngIf="angForm.controls['description'].invalid && (angForm.controls['description'].dirty || angForm.controls['description'].touched)"
class="alert alert-danger">
<div *ngIf="angForm.controls['description'].errors.required">
description is required.
</div>
</div>
<div class="form-group">
<button (click)="addReview(author.value, description.value)" [disabled]="angForm.pristine || angForm.invalid" class="btn btn-primary">Add</button>
</div>
</form>
</div>
Question
what is wrog with my code? any help suggestion will be apreciated
Upvotes: 0
Views: 2246
Reputation: 133
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 });
});
}
Upvotes: 2
Reputation: 57909
@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
Upvotes: 3