2017-02-15 40 views
0

快速的問題(我希望): 我通過數據試圖ng-repeat$scope.discover我用下面的代碼來填補範圍: `吳-重複火力地堡snap.val

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:任何想法,我哪裏出錯了?

回答

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); 
    }); 

該解決方案不住更新,因爲這將需要更多的複雜性,如火力地堡處理等