2

Given an array:

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

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

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Great! Your example helped me understand some thing that made it work for me! Thanks! :)
I'm glad! you should edit the question fitting your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.