当我更改数据库值时,它会反射(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/