2016-09-24 58 views
0

我正在嘗試使用angularfire實現手風琴。我能夠檢索頂層列表(「A1」,「D1」,「R1」)進行顯示,但我無法弄清楚如何檢索選定的每個手風琴選項卡的孩子。例如,如果我選擇「D1」,它應該打開並顯示「C1」,「H1」。如何使用Angularfire動態訪問Firebase數組的子記錄

這裏是我的火力

{ 
    "A1" : { 
    "B1" : 50 
    }, 
    "D1" : { 
    "C1 " : 98, 
    "H1" : 12 
    }, 
    "R1" : { 
    "RR1" : 67, 
    "RD1" : 54 
    } 
} 

我的代碼數據

var app=angular.module("sampleApp",["firebase"]); 
     app.controller("MyCtrl", ["$scope", "$firebaseArray", "$firebaseObject", 
       function($scope, $firebaseArray,$firebaseObject) { 

        var ref = firebase.database().ref("Products/"); 

        var list = $firebaseArray(ref); 
        $scope.list = list; 
        $scope.activeTabs = []; 
        // check if the tab is active 
        $scope.isOpenTab = function (tab) { 
         // check if this tab is already in the activeTabs array 

         if ($scope.activeTabs.indexOf(tab) > -1) { 
          // if so, return true 
           return true; 
         } else { 
         // if not, return false 
           return false; 
         } 
        } 
        // function to 'open' a tab 
        $scope.openTab = function (tab) { 
          // check if tab is already open 
          if ($scope.isOpenTab(tab)) { 
          //if it is, remove it from the activeTabs array 
           $scope.activeTabs.splice($scope.activeTabs.indexOf(tab), 1); 
          } else { 
           // if it's not, add it! 
            $scope.activeTabs.push(tab); 
          } 
        }     
       } 
       ]); 

HTML Code 

    <div class="container accordion__container" ng-controller="MyCtrl"> 

     <div class="accordion__tab" ng-repeat="products in list"> 
      <div class="accordion__tab-title" ng-click="openTab(products.$id)">{{products.$id}} </div> 

      <div class="accordion__tab-content" ng-show="isOpenTab(products.$id)"> 

       <div class="accordion__tab-contentdet" ng-repeat="productDet in <sub Product details>">  


       </div> 
      </div> 


     </div> 
     </div> 

回答

1

我做在你的代碼的一些變化。

HTML我使用了導航選項卡。

<ul class="nav nav-tabs"> 
    <li ng-repeat="products in list"> 
    <a data-toggle="tab" href="#{{products.id}}">{{products.id}}</a> 
    </li> 
</ul> 

<div class="tab-content"> 
    <div id="{{products.id}}" class="tab-pane fade" ng-repeat="products in list"> 
     <h3>{{products.id}}</h3> 
     <p>Content : {{products.data}}.</p> 
    </div> 
</div> 

$()加載控制器

app.controller("MyCtrl", ["$scope", "$firebaseObject", 
    function($scope, $firebaseObject) { 

    var ref = firebase.database().ref("Products"); 
    var list = $firebaseObject(ref); 

    list.$loaded().then(function() { 
     $scope.list = []; 
     angular.forEach(list, function(value,key){ 
      $scope.list.push({ id: key, data: value}) 
     }) 
    }); 
    }    
]); 

另一種方法 而不是使用列表中,您可以使用下面的代碼:

ref.once('value', function(snapshot) { 
    $scope.list = []; 
    angular.forEach(snapshot.val(), function(value,key){ 
    $scope.list.push({ id: key, data: value}) 
    }) 
}) 

我剛剛創建一個爲你而活的人。請檢查

https://plnkr.co/edit/5dOr7xAWIFlmdykAC1yh

,如果您有任何疑問,請讓我知道。

+0

謝謝..我會試試這個。 – kpbeng