I am having trouble sticking to an observer. I have a component which creates an observable, and in the callback, I catch the observer. But later, I cannot get that observer - as if I have another instance of component.
Example:
import {Component} from 'angular2/core';
import {Observable} from 'rxjs/Observable';
@Component({
selector: 'users-table',
templateUrl: './components/users/users-table.html',
// template: '<button (click)="popup({value: 2})">Clickme</button>'
})
export class UsersTableComponent {
public userClicked$: Observable<any>;
public observer: any;
public thing: number;
constructor() {
console.log('Starting UsersTable component');
this.thing = 1;
this.userClicked$ = new Observable(observer => {
this.thing = 2;
console.log('Observer received:', observer);
this.observer = observer;
this.observer.next('Val 1'); // I receive this in an outside component
}).share(); // This supposedly makes the observable *hot*
}
public handle(value) {
console.log('Value:', value);
console.log(this.userClicked$);
console.log('thing:', this.thing); // This is set back to "1
this.observer.next(user); // this.observer is undefined
// so it fails here
}
}
Note that I'm subscribing to this from another component, and I do get that initial test value ('val 1'
)
The other component is listening, subscribing in "ngAfterViewInit", basically like this:
this.usersTableComponent.userClicked$.subscribe(val => { console.log('heard ya:' val); });
Am I missing something obvious? Can I somehow use click handler directly to pass that clicked user thing? (a row in the table)?
Angular2 Beta 8.