Currently learning RxJS. I have an integer selectedCourseIndex within a service that I'd like a separate component to subscribe to.
courses-section.service.ts
private selectedCourseIndex: number = -1; //this number can change from another method
private selectedCourseIndexUpdated = new Subject<number>();
getSelectedCourseUpdateListener(){
return this.selectedCourseIndexUpdated.asObservable();
}
courses-section.component.ts
private selectedCourseIndex: number = -1; //Placeholder initial selectedCourseIndex until new one given on init from courses-section service
private selectedCourseSub: Subscription;
constructor( public coursesSectionService: CoursesSectionService ){}
ngOnInit(){
this.selectedCourseIndex =
this.coursesSectionService.getSelectedCourseIndex();
this.selectedCourseSub = this.coursesSectionService
.getSelectedCourseUpdateListener()
.subscribe((selectedCourseIndex: number) => {
this.selectedCourseIndex = selectedCourseIndex;
console.log('this fires when subscription updates'); //this never fires once
});
}
However, the subscription never fires, either when it initally subscribes or when the integer changes. Whats wrong?