Am trying to implement observable with the following class
import {Injectable, } from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/share';
import {Observer} from 'rxjs/Observer';
@Injectable()
export class CalendarService {
dataChange: Observable<any>;
dataChangeObserver: Observer<any>;
constructor() {
this.dataChange = new Observable((observer) => {
this.dataChangeObserver = observer;
}).share();
}
setDate(date: any) {
this.dataChangeObserver.next(date);
}
}
When calling setDate() with a value am getting the following error
ORIGINAL EXCEPTION: TypeError: Cannot read property ‘next’ of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property ‘next’ of undefined
It seems that this.dataChangeObserver is not defined, am I missing something?