Changes in Observable not reflected in View

后端 未结 3 736
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 19:30

I basically followed this guide to implement an Observable data service.

In a store class (ItemsStore), I have my BehaviorSubject which hol

3条回答
  •  灰色年华
    2021-01-19 19:55

    I'm suspicious the problem is in the way you call:

    this.items.next(this.item.getValue().push(newItem));
    

    I don't know what this.item.getValue() returns but if push() method is the same as Array.push() it returns new length (the important thing is it doesn't return this.items with the new item appended).

    The async pipe is defined as:

    The async pipe subscribes to an Observable or Promise and returns the latest value it has emitted.

    So when you call this.items.next(...) the async pipe receives just the new length and tries to iterate it with *ngFor.

    If this.item holds > then you probably want to call:

    this.item.getValue().push(newItem);
    this.items.next(this.item);
    

    Btw, the asObservable() method is used to hide the fact that you're working with Subject. Subject let's you call next() or complete() which is something that you don't want other users to mess with. For this reason it's better to pass everywhere just an Observable and keep Subject only for yourself where you know you need it.

提交回复
热议问题