Willwsharp
Willwsharp

Reputation: 723

Async Pipe failing when linked to newly created observable

When attaching an Async Pipe to a newly created Observable, you get the error

"Cannot read property 'subscribe' of undefined"

Here is a Plunkr demonstrating what I'm talking about: https://plnkr.co/edit/vljXImCYoNubjyxOaWo3?p=preview

However, if I comment out

private asyncList: Observable<string[]> = new Observable<string[]>();

and un-comment

//private asyncList: Observable<string[]>;

then the page will load without a problem. Maybe I'm missing something blatantly obvious but this is throwing me for a loop.

This Plunkr was forked from someone else's and I did the least amount of work to demo the error, so please excuse any weird naming or practices you see.

Upvotes: 1

Views: 230

Answers (1)

martin
martin

Reputation: 96969

Using new Observable() is the same as calling Observable.create() which means you need to supply it with a "subscription" function that emits values to the observer on subscription. For example:

private asyncList = new Observable<string[]>(o => o.next('Hello'));

Notice that the error in your demo thrown in the Observable and not in the pipe itself.

I'm actually surprised that the parameter in the Observable class is optional. I'm not aware of any use-case where you might ignore it...

Upvotes: 2

Related Questions