ANGULAR 如何推送到可观察数组?英雄 : Observable<Hero[]>

标签 angular typescript firebase rxjs angularfire

给定一个数组:

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

你会如何做类似的事情:hero.push(newHero)?

最佳答案

不确定您想要实现什么目标,但自从您标记了 firestore 以来。我想你可以使用 angularfirestorecollection 并稍后将它们映射到可观察数组。

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

在初始化时,您会将它们绑定(bind)在一起。

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;
    });
  });
}

然后您可以将卡片添加到 AngularFirestoreCollection。

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

希望这有帮助。如果您想查看我的完整代码和项目,请访问我的 github .

关于ANGULAR 如何推送到可观察数组?英雄 : Observable<Hero[]>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50181350/

相关文章:

angularjs - 在 firebase 中使用 $location.path() 进行重定向

javascript - 无法在指令 angularjs 中从 firebase 获取数据

Angular 导入的模块不等待 APP_INITIALIZER

mysql - 无法使用 Angular 和 Sequelize 在 MariaDB sql server 中运行两个查询(语法错误)

javascript - 使用 Firebase,firebase.auth().currentUser 是 null 值,即使它正在被记录

jquery - 使用 Typescript 时如何停止 "property does not exist on type JQuery"语法错误?

javascript - 在 Angular 8 中加载页面时,CSS 会分散

javascript - 如何使用 Node.js 服务器部署 Angular 5 应用程序

javascript - 如何在前端和后端之间共享 TypeScript/JavaScript?

ios - 如何使用 Firebase 在 UITableViewController 中正确显示数据?