Firebase snapshot.val() 未绑定到 $scope
Firebase snapshot.val() not binding to $scope
我正在使用 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);
但我仍然想知道我做错了什么以及为什么它不能使用快照。
// 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" 的解决方案
我正在使用 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);
但我仍然想知道我做错了什么以及为什么它不能使用快照。
// 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" 的解决方案