PillowPillow/ng2-webstorage

Subscribing in template using async pipe

Closed this issue ยท 5 comments

I define the Observable as

this.cart$ = this.storage.observe('cart');

and then in the template I just subscribe {{(cart$ | async | json)}} but I get null.

Note: If I try to fetch the value using a decorator @LocalStorage('cart') cart: Cart;
I see that the property cart does contain what I expect..

Did some more testing...
even if I'm subscribing I don't get an initial value..

this.storage.observe('cart').subscribe(e => console.log('e', e));

This will only console log the value on an update.. no init value...

Guess it has something to do with filters?

filter((changed: string) => changed === null || changed === key),

@PillowPillow is this project dead or alive? :)

Hello @mackelito , Yes this project's still alive since we're using it in production in my company' projects.
What you are facing of is the expected behavior. The observe() method act as cold observable.
If you don't wanna use the decorators to bind your property you have to use the startWith operator to setup the start value.

this.storage.observe('cart').pipe(
    startWith(this.storage.retrieve('cart'))
).subscribe(e => console.log('e', e));

Glad to hear it's still alive ๐Ÿ‘

I had totally forgotten about the concept of hot/cold observables.. ๐Ÿ˜„ thx for the reminder!