angularjs - Firebase snapshot.val() 未绑定(bind)到 $scope

标签 angularjs firebase firebase-realtime-database angularfire

我正在使用 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/

相关文章:

javascript - AngularJS:REST API 调用的表达式值

angularjs - typescript 命名参数就像在angularjs中使用的那样?

javascript - 如果 ng-click 附加到提交按钮,则表单不验证

javascript - 在 javascript 中使用 angular.js 的数据绑定(bind)/或更新事件上的 javascript 变量

android - 出现错误 “Execution failed for task ':app:compileDebugJavaWithJavac'”

nested - Firebase 嵌套查询

android - 在 onActivityResult() 中获取 Firebase 邀请数据

javascript - 循环数据以提取 Firebase 值数组

java - 使用 Firebase 数据库填充 RecyclerView

ios - 我想将 bool 状态保存到 Firebase 数据库中的用户配置文件