Why am I unable to successfully initialize and retrieve data from a service using a subject in Angular?
HomeComponentComponent.TS
export class HomeComponentComponent implements OnInit {
public homeSub;
constructor(
private subService: SubjectService
) { }
ngOnInit() {
this.subService.initialiseSubject(true);
this.subService.getSubject().subscribe(res => {
this.homeSub = res;
console.log(res);
})
}
}
HomeComponentComponent.HTML
<p>
{{homeSub}}
</p>
subject.service.ts
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class SubjectService {
constructor() { }
private subTest = new Subject<boolean>();
initialiseSubject(params: boolean) {
this.subTest.next(params);
}
getSubject() {
return this.subTest.asObservable();
}
}
I am facing issues as no output is being displayed on the console or HTML.
View Stackblitz example