我正在使用 FireBase 并尝试执行一些查询,结果正在登录,但在 HTML $scope
中不可见。
var shopRef = firebaseDataService.intro;
$scope.shops = [];
var taskRef = shopRef.orderByChild("cat").equalTo("Accomodation");
taskRef.on("value", function(snapshot) {
var snapData = snapshot.val();
console.log(snapData);
$scope.shops.push(snapData);
});
当我使用 $scope.$apply()
时,我设法将数据更新到商店,但它仍然没有将任何内容传递给我的指令。
<search-card shops="shops"> </search-card>
<p> Shops are {{shops}}</p>
我让它以某种方式与 $firebaseArray 一起工作
$scope.shops = $firebaseArray(taskRef);
但我仍然想知道我做错了什么以及为什么它不能与快照一起使用。
最佳答案
来自angularfire docs :
// read data from the database into a local scope variable
ref.on("value", function(snapshot) {
// Since this event will occur outside Angular's $apply scope, we need to notify Angular
// each time there is an update. This can be done using $scope.$apply or $timeout. We
// prefer to use $timeout as it a) does not throw errors and b) ensures all levels of the
// scope hierarchy are refreshed (necessary for some directives to see the changes)
$timeout(function() {
$scope.data = snapshot.val();
});
});
似乎使用 $scope.apply()
不会刷新整个层次结构(因此也刷新指令)。尝试按照规定使用 $timeout
话虽这么说,我认为你应该使用 $firebaseArray()
选项,因为我认为这是最“有 Angular ”的解决方案
关于angularjs - Firebase snapshot.val() 未绑定(bind)到 $scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38310267/