Ng-repeat Firebase snap.val

时间:2017-02-15 00:30:53

标签: angularjs firebase firebase-realtime-database

快速提问(我希望): 我正在尝试通过我ng-repeat中的数据$scope.discover我使用以下代码来填充该范围: `

const businessKey = '-KQ1hFH0qrvKSrymmn9d';
                const rootRef = firebase.database().ref();
                const discoverRef = rootRef.child('discover');
                const businessRef = rootRef.child('businesses');


                function getDiscoverBarContent(key, cb) {
                    discoverRef.child(key).on('child_added', snap => {
                        let businesRef = businessRef.child(snap.key);
                        businesRef.once('value', cb);
                    });
                }

                getDiscoverBarContent(businessKey, snap => {
                    console.log(snap.val());
                    $scope.discover = snap.val();
                });`
And then a simple ng-repeat:
`<a ng-repeat="item in discover" >{{item.name}}</a>`

然而,这不起作用并返回一行代码:confused:任何我想出错的想法?​​

1 个答案:

答案 0 :(得分:0)

我很接近但需要创建一个空的发现数组并将捕捉结果推送到该范围数组。请参阅以下修改后的代码:

$scope.discover = [];
    const businessKey = $scope.finalItem.$id;
    const rootRef = firebase.database().ref();
    const discoverRef = rootRef.child('discover');
    const businessRef = rootRef.child('businesses');


    function getDiscoverBarContent(key, cb) {
        discoverRef.child(key).on('child_added', snap => {
            let businesRef = businessRef.child(snap.key);
            businesRef.once('value', cb);
        });
    }

    getDiscoverBarContent(businessKey, snap => {
        var snapVal = snap.val();
        $scope.discover.push(snapVal);
    });

此解决方案不是实时更新,因为这需要更多复杂性,例如Firebase处理等。