0

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).

Bharat
  • 2,441
  • 3
  • 24
  • 36
aslani
  • 1

1 Answers1

0

Please check my answer of Question can i emmit the event from parent to child to see how to set Observable and how to subscribe to it.

Community
  • 1
  • 1
ranakrunal9
  • 13,320
  • 3
  • 42
  • 43