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 the Observer dataChangeObserver
is not defined. But when debugging i can see that dataChangeObserver
value i set. But Ionic 2 somehow forgets about it when calling setDate(date: any)
.