While I understand that this question has been asked numerous times on SO and other websites, I find myself getting confused by the varying answers.
2 INQUIRIES -
- Should I subscribe to my component in the constructor() or NgOnInit()?
- Is it necessary to use a pipe when subscribing to an Observable for Angular to handle the destruction, or can I avoid using ngOnDestroy? I'm puzzled by the presence of pipes after subscribing.
Let me illustrate how I am implementing one of my services; specifically, in my navbar component where I subscribe to changes in window size from a service.
In the constructor, I have implemented the following code:
this.responsiveService.getMobileStatus()
.subscribe(mobileStatus => {
this.isMobile = mobileStatus.status;
if (mobileStatus.width < 568) {
this.inputPlaceholder = this.placeholderWithSearch;
} else {
this.inputPlaceholder = this.placeholderWithoutSearch;
}
});