Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I append items to Observable

Tags:

How do I append items to Observable?

This is some code:

 this.logEntries = this.controllerService.getLog(this.controller.remoteID, this.skip, this.max);          this.logEntries.subscribe(a => {             this.allLogEntries.add(this.logEntries)         }); 

and here is their declarations:

 logEntries: Observable<Log[]>;     allLogEntries: Observable<Log[]> = Observable.empty<Log[]>(); 

I want to append items to allLogEntries as they are being fetched from the web service. How do I do this?

like image 808
Tim Liberty Avatar asked Nov 21 '16 11:11

Tim Liberty


People also ask

Can you subscribe to an Observable?

SubscribinglinkAn Observable instance begins publishing values only when someone subscribes to it. You subscribe by calling the subscribe() method of the instance, passing an observer object to receive the notifications.

What is difference between subscribe and Observable?

Observables are not executed until a consumer subscribes. The subscribe() executes the defined behavior once, and it can be called again. Each subscription has its own computation. Resubscription causes recomputation of values.


2 Answers

Perhaps the scan operator could interest you. Here is a sample:

obs.startWith([])  .scan((acc,value) => acc.concat(value))  .subscribe((data) => {    console.log(data);  }); 

See this question for more details:

  • Angular 2 Updating objects in “real time.”
like image 158
Thierry Templier Avatar answered Oct 20 '22 02:10

Thierry Templier


Take a look at BehaviorSubject:

It's an Observable where you can push a new item as containing object.

like image 22
Ramazan Gevrek Avatar answered Oct 20 '22 01:10

Ramazan Gevrek