2016-08-19 29 views
0

我想在我的網站上做一個評論部分。我已正確鏈接了所有文件,並從Firebase獲取數據正在運行。但是,評論僅在之後加載我要麼點擊某個內容,要麼在「添加評論」字段中輸入文本;它不會在頁面加載時立即顯示評論。有誰知道爲什麼?立即使用AngularJS從Firebase加載數據

相關代碼:。(放在我的控制器的頂部)

$scope.comments = {}; 
database.ref('comments').on('value', function(items) { 
    $scope.comments = items.val(); 
}); 
$scope.comment = function() { 
    database.ref('comments').push($scope.newComment); 
    $scope.newComment = ""; 
}; 
+0

見http://stackoverflow.com/questions/35531161/採取長時間加載/ 35533455#35533455 –

回答

1

使用$範圍$申請

$scope.comments = {}; 
database.ref('comments').on('value', function(items) { 
    $scope.$apply(function() { 
     $scope.comments = items.val(); 
    }); 
}); 
$scope.comment = function() { 
database.ref('comments').push($scope.newComment); 
    $scope.newComment = ""; 
}; 
+1

您應該添加一些解釋。 –

+0

@Aditya:我同意尼古拉斯。這解決了我的問題,但是請你解釋'$ scope。$ apply'的作用嗎? –

相關問題