【问题标题】:Loop through observable<Object[ ]> in Typescript在 Typescript 中循环遍历 observable<Object[ ]>
【发布时间】:2018-10-09 21:41:21
【问题描述】:

我有一个 firebase 数据库...所以都是 JSON。 我使用 AngularFire2 数据库检索数据...我正在阅读 this 教程...

本题部分代码为:

noteList: Observable<Note[]>

  constructor(public navCtrl: NavController, private noteListService: NoteListService) {
    this.noteList = this.noteListService.getNoteList()
      .snapshotChanges()
      .map(
      changes => {
        return changes.map(c => ({
          key: c.payload.key, ...c.payload.val()
        }))
      });

从这里我想做两件事:

  • 1st:在构造函数之外声明一个数组变量,就像noteList 例如:myVar: Note[] = new Array();
  • 2nd :虽然getNoteList() 的结果是可迭代的,并将它们推入该变量以供将来使用...我希望myVar 拥有Note 的多个对象...=> 这些是来自Firebase 的JSON因此 JavaScript 对象...

我该怎么做?

到目前为止,我使用的是以下内容:

this.noteList .forEach(value =&gt; console.log(value)); 这会将 noteList 的每个元素记录为 Array [Object] ....

当我执行this.noteList .forEach(value =&gt; this.myVar.push(value)); 时,它会说:

'Note[]' 类型的参数可分配给'Note' 类型的参数。 “Note[]”类型中缺少属性“id”

完整的类代码是:

export class HomePage {

  noteList: Observable<Note[]>
  myVar : Note[] = new Array();

  constructor(public navCtrl: NavController, private noteListService: NoteListService) {
    this.noteList = this.noteListService.getNoteList()
      .snapshotChanges()
      .map(
      changes => {
        return changes.map(c => ({
          key: c.payload.key, ...c.payload.val()
        }))
      });
  }

  this.noteList .forEach(value => this.myVar.push(value));

  //Then below, I want to use it in a Jquery 

  $(function() {
     // Also, I don't know how to access neither noteList nor myVar here. Any help here will be appreciated also
  }
}

你能帮我做这件事吗...

【问题讨论】:

  • 问题可能是XY问题。您正在尝试将现代方法(这是 Angular+Ionic,不是吗?)与老式的“jQuery 编程”相结合。永远不应该这样做。由于多种原因,jQuery 在 Angular 应用程序中是绝对禁忌的。关于 Then below我不知道如何在此处访问 noteList 和 myVar,这是 stackoverflow.com/questions/14220321/… 的欺骗。您需要接受 observables,而不是为您可能更熟悉的 jQuery 创建桥梁。您可以访问 observable .subscribe 中的值
  • @estus 哦,告诉我...编程已经走了很长一段路,我知道我需要更新我的技能...我会考虑您对 jquery 的建议...但是对于.subscribe 部分,您的任何提醒将不胜感激....我将如何迭代 .subscribe 结果以允许我使用 Array.push 方法构造 myVar
  • 你不需要迭代它。这是.subscribe(notes =&gt; { this.myVar = notes })
  • @estus 谢谢你,伙计......它工作......如果你想投票给我的OP,你可以添加它作为答案;-)

标签: angular typescript rxjs observable


【解决方案1】:

使用 RxJS 执行此操作的正确方法是订阅 observable 并在 subscribe 回调中使用 result 执行所有必要的操作。如果未重用 observable,则无需将其保存到 noteList,但可能需要保存订阅才能取消订阅并避免内存泄漏:

  noteListSubscription: Subscription;

  constructor(private noteListService: NoteListService) {
    this.noteListSubscription = this.noteListService.getNoteList()
      .snapshotChanges()
      .map(
      changes => {
        return changes.map(c => ({
          key: c.payload.key, ...c.payload.val()
        }))
      })
      .subscribe(notes => { this.myVar = notes });
  }

  ngOnDestroy() {
    noteListSubscription.unsubscribe();
  }

Subscription 类型应从rxjs 模块导入。

【讨论】:

  • noteListSubscription 何时/如何实例化?
  • 有一个错字,已修正。
  • 是的...现在可以正常工作了...谢谢伙计。不要忘记为Subscription type 指明import...谢谢你的伙伴,你帮了我很多。请投票支持我的问题,或者帮助我改进它;-)
猜你喜欢
  • 1970-01-01
  • 2020-11-24
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 2013-12-28
  • 2021-08-27
  • 2015-03-20
相关资源
最近更新 更多