我正在我的应用程序中创建评论部分,当用户输入评论并提交时,评论应立即显示在前端,不幸的是,现在只有在刷新后才能看到评论,
这就是我所拥有的:
显示评论
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>
**
添加评论:
** 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>
问题
我的代码是什么?任何帮助建议都会被重视
答案 0 :(得分:2)
@Kaczkapojebana。当我们订阅“获取”时,不意味着视图中显示了数据库中的更改。 (仅说异步调用完成后,将显示数据)。我通常将“获取”订阅称为“仅一次使用的订阅”。
您必须手动添加到此。对新数据进行注释。哪里? IN订阅功能:
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 });
});
}
注意:您也可以再次订阅以获取所有数据,或者addReview响应所有数据 注意2:同样,进行更新时,必须手动更新“ this.comments”数组
答案 1 :(得分:1)
您必须在提交评论的回调中调用getComments
方法。您的代码仅在组件的ngOnInit
方法中具有“获取”调用。因此,当刷新视图时,ngOnInit
将再次执行,因此将调用getComments
。
您必须在提交评论方法的回调中进行一次get调用。
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 });
});
}