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

pec*_*ata 2 angularjs firebase angularfire firebase-realtime-database

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

});
Run Code Online (Sandbox Code Playgroud)

当我使用时$scope.$apply(),我设法将数据更新到商店,但它仍然没有将任何内容传递给我的指令.

    <search-card shops="shops">   </search-card>
    <p> Shops are {{shops}}</p>
Run Code Online (Sandbox Code Playgroud)

我用$ firebaseArray以某种方式工作了

  $scope.shops = $firebaseArray(taskRef);
Run Code Online (Sandbox Code Playgroud)

但是我仍然想知道我做错了什么,以及为什么它没有使用快照.

Vas*_*rth 5

来自angularfire文档:

// 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();
    });
});
Run Code Online (Sandbox Code Playgroud)

似乎使用$scope.apply()不会刷新整个层次结构(因此指令).尝试$timeout按照规定使用


话虽这么说,我认为你应该选择这个$firebaseArray()选项,因为这是最"有棱角"的解决方案