angular - 我想停止 Angular-2 中 Firebase 的实时数据反射

标签 angular firebase firebase-realtime-database angularfire2

当我更改数据库值时,它会反射(reflect)在我的页面中而无需刷新。所以我想停止实时数据反射。

constructor(public af: AngularFireDatabase,
    public route: ActivatedRoute) {
    this.addemployee = af.list('/employees');
    this.employee = af.object('/employees');
}

getEmployees() {
    this.employees = this.af.list('/employees') as FirebaseListObservable<any[]>;
    return this.employees;
}

最佳答案

解决方案: 添加take(1)所以该行是 this.employees = this.af.list('/employees').take(1) as FirebaseListObservable<any[]>;

如果我理解正确的话。您不需要实时更新,而只需要一次数据。

您可能正在使用:https://firebase.google.com/docs/reference/js/firebase.database.Reference#on 这就是为您提供数据更新的原因。

ref.on('value', function(dataSnapshot) {
  // ...
});

如果您只需要一次数据,可以使用:https://firebase.google.com/docs/reference/js/firebase.database.Reference#once

// Basic usage of .once() to read the data located at ref.
ref.once('value')
  .then(function(dataSnapshot) {
    // handle read data.
  });

更新:添加了非常简单的 JavaScript 示例,来自评论的请求。

var ref = firebase.database().ref('database/path/to/something');
ref.once('value')
  .then(function(dataSnapshot) {
    // handle read data.
    let data = dataSnapshot.val();
    // data is { "name": "Ada", "age": 36 }
    // data.name === "Ada"
    // data.age === 36
  });

关于angular - 我想停止 Angular-2 中 Firebase 的实时数据反射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45615057/

相关文章:

javascript - 放置在 "Dist"文件夹中的文件位置错误,破坏了 Angular 2 应用程序

angular - ionic 3 : How to add a component

javascript - 从公共(public)文件调用 Angular Observable.subscribe()

angular - 错误 : NullInjectorError: No provider for FlashMessagesService

firebase - 注销时取消订阅 Firestore 监听器

ios - 从函数返回之前等待 Firebase 加载

android - 我需要帮助以将按照我的 'Document' 列表排序的 'Collection' 数据显示到 RecyclerView 中(检索到的数据未按照列表排序,#AskFirebase)

android - 如何从 firebase 数据库中获取 child 总数

Swift Firebase 4.0-observeSingleEvent 不返回数据集(但身份验证正在工作)

firebase - 当用户关注某人时如何更新 Firebase 数据库中的用户 Feed