当我更改数据库值时,它会在我的页面中反映而不刷新。所以我想停止实时数据反映。
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;
}
答案 0 :(得分:1)
<强>解决方案:强>
添加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
});