【问题标题】:Firebase query on.(child_added) execute function only after dataset is loadedFirebase 查询 on.(child_added) 仅在加载数据集后执行功能
【发布时间】:2015-06-23 23:16:18
【问题描述】:

我有一个 Angularjs htmlfile,它列出了来自 firebase 的数据。来自 firebase 的数据使用 ng-repeat 呈现。页面加载时,会显示数据。

试图弄清楚如何在将孩子添加到数据集时触发事件。

Firebase web api 导致以下代码:

var ref = new Firebase("https://test.firebaseio.com/test");

$scope.test= $firebaseArray(ref);  

    ref.on('child_added', function (childSnapshot, prevChildKey) {

function_to_execute();

});

问题是function_to_execute()pageload 以及添加的每个子项上都会触发。

有没有办法加载数据集,只有在加载数据集后实际添加child时才触发函数?

【问题讨论】:

  • 你能添加更多代码吗?您现在显示的代码是在控制器中还是在其他地方?

标签: angularjs firebase angularfire


【解决方案1】:

你可以挂钩 $loaded 承诺:

var ref = new Firebase("https://test.firebaseio.com/test");

$scope.test= $firebaseArray(ref);  

ref.$loaded()
    .then(function(){
        ref.on('child_added', function (childSnapshot, prevChildKey) {
            function_to_execute();
        });
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    相关资源
    最近更新 更多