2

Given an array:

// hero.service.ts
hero: Observable<Hero[]>;

How would you do something similar to: hero.push(newHero)?

iSebbeYT
  • 1,441
  • 2
  • 18
  • 29
  • 2
    Possible duplicate of [RxJS: How would I "manually" update an Observable?](https://stackoverflow.com/questions/33324227/rxjs-how-would-i-manually-update-an-observable) – Vikas May 04 '18 at 18:43

1 Answers1

2

Not sure what you are trying to achieve, but since you tagged firestore. I guess you could use angularfirestorecollection and map them to observable array later.

cardsCollection: AngularFirestoreCollection<Card>;
  cardDoc: AngularFirestoreDocument<Card>;
  cards: Observable<Card[]>;

On init you will bind them together.

ngOnInit() {
  this.cards = this.cardsCollection.snapshotChanges().map(changes => {
    return changes.map(a => {
      const data = a.payload.doc.data() as Card;
      data.id = a.payload.doc.id;
      return data;
    });
  });
}

and then you can add cards to AngularFirestoreCollection.

addCard(card: Card) {
    this.cardsCollection.add(card);
}

Hope this helps. If you want to see my full code and project go to my github.

GreedyAi
  • 2,709
  • 4
  • 29
  • 55