Not sure if relevant or if it helps someone: I ran into a similar issue: I wanted to consume a property of type Observable<number>
provided by a component in the parent component.
After reading Observables and Reactive Programming in Angular 2, I noticed that I need to "turn" around my architecture and let the parent create the Observable
and then assign it to the child.
So instead of having a MyComponent
with an @Output()
of type Observable<number>
(which was then initialized to new BehaviorSubject(0)
and modified using calls to next
), I changed MyComponent
to haven an @Input()
of type BehaviorSubject<number>
:
@Component({
selector: 'my-component',
template: `
<button (click)="increment()">Click me!</button>
`,
})
export class MyComponent {
private _count : number = 0;
@Input()
public counter : BehaviorSubject<number>;
public increment() {
this._count++;
if (this.counter)
this.counter.next(this._count);
}
}
The parent now creates a BehaviorSubject and binds to the property. It can easily consume the BehaviorSubject as an Observable:
@Component({
selector: 'my-app',
template: `
<my-component [counter]="count"></my-component>
<span>{{text | async}}</span>
`,
})
export class App {
public text : Observable<string>;
public count : BehaviorSubject<number> = new BehaviorSubject(0);
constructor() {
this.text = this.count.map(n => "Click: " + n);
}
}
Plunker: https://plnkr.co/edit/rKtVWmmGnFn4Po4B4CCL?p=preview
So in your case, I would say that your slider should provide an @Input
(maybe call it value or number) and let the parent assign an observable (instead of letting the child create one).