Using an array from Observable Object with ngFor and Async Pipe Angular 2

后端 未结 4 529
南笙
南笙 2020-12-08 03:25

I am trying to understand how to use Observables in Angular 2. I have this service:

import {Injectable, EventEmitter, ViewChild} from \'@angular/core\';
i         


        
4条回答
  •  有刺的猬
    2020-12-08 04:04

    If you don't have an array but you are trying to use your observable like an array even though it's a stream of objects, this won't work natively. I show how to fix this below assuming you only care about adding objects to the observable, not deleting them.

    If you are trying to use an observable whose source is of type BehaviorSubject, change it to ReplaySubject then in your component subscribe to it like this:

    Component

    this.messages$ = this.chatService.messages$.pipe(scan((acc, val) => [...acc, val], []));
    

    Html

提交回复
热议问题