【问题标题】:I need a listener to see if my notifications table (node) has changed for real time data checking我需要一个监听器来查看我的通知表(节点)是否已更改以进行实时数据检查
【发布时间】:2016-05-14 00:45:54
【问题描述】:

Firebase - 我需要一个监听器来查看我的通知表(节点)是否已更改以进行实时数据检查。

我在我的项目中使用带有 typescript 和 firebase 的 angular2。

【问题讨论】:

标签: typescript firebase angular


【解决方案1】:

你可以使用类似的东西:

export class AppComponent {
  items:Array<string>;

  constructor() {
    var firebaseURL = "https://something.firebaseio.com/somethingelse";
    var myFirebaseRef = new Firebase(firebaseURL);

    myFirebaseRef.on('child_added', (childSnapshot, prevChildKey) => {
      childSnapshot.forEach((records) => {
        this.items = itemsarr.push(records.val());
      });
      console.log(itemsarr)
    });
    console.log(itemsarr);
  }
}

注意在组件中实例化Firebase 对象,以使Angular2 知道items 的变化。

希望对你有帮助 蒂埃里

这是我当前的代码,但在将通知添加到表格时它不会触发:

waitingForNotificationChanges = (): void => {
    this.refNotifications.on('child_added', (childSnapshot, prevChildKey) => {          
        console.log("Notification table changed");
        this.getNumberOfNotificationsByUid();
    });
};

【讨论】:

  • 谢谢蒂埃里,我现在就试试
  • 我在上面添加了你给我的当前代码。由于某种原因它没有触发
  • 如何以及何时调用waitingForNotificationChanges 方法?
  • 我从不叫它。我需要将其移至构造函数吗?
  • 这是个问题 ;-) 因为这个方法负责在 Firebase 上注册你的回调。您应该从构造函数调用它,例如:constructor() { (...) this.waitingForNotificationChanges(); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多