【发布时间】:2017-12-06 11:50:33
【问题描述】:
我有 2 个组件,一个 Moment(post/thread) 和一个 Comment,每个 moment 可以有多个 cmets。
在moment-details.component 中,我插入了一个moment-comment.component。评论组件显示与该时刻相关的所有 cmets。
moments-detail.component.html
<div id="moment">
...
<app-moment-comment [moment]="moment" [auth]="auth" [comment]="comment" class="comment" *ngFor="let comment of moment?.comments; let i=index">
</app-moment-comment>
</div>
</div>
</div>
moment-detail.component.ts
@Component({
selector: 'app-moment-detail',
templateUrl: './moment-detail.component.html',
styleUrls: ['./moment-detail.component.scss']
})
export class MomentDetailComponent implements OnInit {
moment: Moment;
constructor(private router: Router,
private route: ActivatedRoute,
private momentService: MomentService,
private authService: AuthService,
private commentService: CommentService) { }
ngOnInit() {
this.route.params.switchMap((params) => {
let moment_id = params['id'];
return this.momentService.get(moment_id);
}).subscribe((res) => {
console.log(res);
this.moment = res;
this.user = res.author;
});
this.authService.getAuthenticatedUser().subscribe((res) => {
this.auth = res;
});
}
}
moment-comment 循环遍历所有 cmets 并创建一个新的 moment-comment 组件实例。
现在在 moment-comment.component.ts 中,用户可以触发删除他们的评论。
@Component({
selector: 'app-moment-comment',
templateUrl: './moment-comment.component.html',
styleUrls: ['./moment-comment.component.scss']
})
export class MomentCommentComponent implements OnInit {
@Input() comment: Comment;
@Input() auth: any;
@Input() moment: any;
showReply: any;
reply: any;
showReplies: boolean;
inEdit = false;
editComment: any;
constructor(private commentService: CommentService) { }
...
delete() {
this.commentService.delete(this.moment._id, this.comment._id).subscribe((res) => {
console.log(res);
}, (err) => {
console.log(err);
});
}
...
}
果然,当用户触发 delete() 函数时,后端会更新我的数据库以显示评论已被删除。
我的问题是,如何在前端(即父级(时刻细节)视图)中反映这种变化?
【问题讨论】:
-
更新对象以更新
ui -
@Aravind ye 我猜是这样的,但是从 moment-comment.component 中,我无法访问整个
moment?.comments,正如您在 ngFor 中的 moment.component 中看到的那样.我每次都通过 1 条评论。 -
您能否在
plunker或stackblitz中重现该错误,以便我帮助您修复它 -
你现在怎么样了?.cmets??
-
能把moment.detail组件ts也发一下吗?
标签: angular