Here is an Angular component code snippet:
export class ListComponent implements OnInit {
showComments: boolean = false;
posts$: Observable<PostModel[]>;
constructor(private postService: PostService) { }
ngOnInit() {
this.posts$ = this.getPosts();
}
getPosts(): Observable<PostModel[]> {
return this.postService.getRecentPosts();
}
}
Is it necessary to define posts$
within the constructor?
What should be the default value for showComments
? Is it acceptable to define it inside ngOnInit
as well?
If not, when is the appropriate time to use the ngOnInit
method?