AngluarJS和Firebase,使用服务更新表

时间:2022-11-06 15:23:53

I have a table I want to update with data I get from my firebase database. I know the firebase calls return a promise and the database call in then ran async, but even in the .then methods I can't seem to extract the async data into my table view.

我有一个表,我想用我从firebase数据库获得的数据更新。我知道firebase调用返回一个promise并且数据库调用然后运行异步,但即使在.then方法中,我似乎无法将异步数据提取到我的表视图中。

To overcome the async part I have tried to used services (based on this answer Update scope value when service data is changed). I used the test callback method addValue()to test adding data. But when I use this callback inside the .then() of the firebase queries the data of the service is updated but it is not updated in the view.

为了克服异步部分,我尝试使用服务(基于此答案更新服务数据时更新范围值)。我使用测试回调方法addValue()来测试添加数据。但是当我在firebase查询的.then()中使用此回调时,服务的数据会更新,但在视图中不会更新。

routerApp.controller('leaderboardCtrl', function($scope, service) {

            $scope.data = service.data;

            service.getLeaderboard('Test1', 'TestGroup', 10);
            //service.getValues();


}).factory("service", function($rootScope, $q)
{
  return new (function AsyncTest(){
    this.data=[
        {name : "Sam" , score : 190}
    ]
    this.getValues=function()
    {
      var self=this;
        //self.data.push({name : "Sam" , score : 180});
        self.addValue();
        self.count++
    },
    this.count=0,

    this.addValue  =function(){
        var self=this;
        self.data.push({name : "Sam" , score : 180});
    }

    this.getLeaderboard = function(test, groupName, limit){
        var self=this;
        var uid = currentUser.uid;



     firebase.database().ref('/groupMembers/' + groupName).once('value').then(function(snapshot) {
        for(var user in snapshot.val()){
            firebase.database().ref('/tests/' +test + '/users/' + user).orderByChild("score").limitToLast(limit).once('value').then(function (snapshot) {
                       console.log('----LEADERBOARD----');
                       console.log(snapshot.val());
                       self.addValue();
                       console.log(self.data);

             });
            }
        });
    }
  })();

});

1 个解决方案

#1


1  

This occurs often in Angular when using firebase. In your addValue() method try adding $scope.apply() after your push logic .

使用firebase时,这通常发生在Angular中。在你的addValue()方法中尝试在推送逻辑之后添加$ scope.apply()。

Example:

this.addValue=function(){
    var self=this;
    self.data.push({name : "Sam" , score : 180});
    $scope.apply();
}

$scope.apply() is used to update the view to reflect whats been done in your controller .

$ scope.apply()用于更新视图以反映在控制器中完成的操作。

Hope this helps

希望这可以帮助

#1


1  

This occurs often in Angular when using firebase. In your addValue() method try adding $scope.apply() after your push logic .

使用firebase时,这通常发生在Angular中。在你的addValue()方法中尝试在推送逻辑之后添加$ scope.apply()。

Example:

this.addValue=function(){
    var self=this;
    self.data.push({name : "Sam" , score : 180});
    $scope.apply();
}

$scope.apply() is used to update the view to reflect whats been done in your controller .

$ scope.apply()用于更新视图以反映在控制器中完成的操作。

Hope this helps

希望这可以帮助