如何将我的子对象推送到数组?

时间:2017-03-13 11:14:40

标签: javascript angularjs arrays firebase

我正在从Firebase中检索数据,当然我得到的是对象:

enter image description here

我可以轻松地在我的Ng-Repeat中显示它们,这不是问题,但是如果我想用输入搜索字段和简单的过滤器搜索它,它会返回错误,因为它没有&#39 ; t接收数组但对象。

我决定将它们发送到这样的数组中:

var userId = firebase.auth().currentUser.uid;
var ref = firebase.database().ref('/accounts/' + userId + "/cards/");

var cards2 = [];
ref.orderByChild('cards').on("value", function(snapshot1) {
  var cards1 = snapshot1.val();
  cards2.push(cards1);
  console.log(snapshot1.val());
  $timeout(function(){
  $scope.cards = cards2;
  console.log(cards2);
  })
})

但是输出是这样的: enter image description here

换句话说,我无法显示我的ng-repeat,因为这两个对象被作为子对象推送到对象中的数组中!

我如何单独对它们进行排序,以便在我的ng-repeat中显示它们并且能够像这样过滤它们:

ng-repeat="card in cards | filter:searchText"

编辑:这是我的HTML代码:

<ion-view view-title="MY CARDS">
    <ion-nav-buttons side="right">
        <button class="button button-icon icon ion-android-more-vertical"></button>
    </ion-nav-buttons>
    <ion-content class="padding">
     <div class="list list-inset input-search">
            <label class="item item-input">
                <i class="icon ion-search placeholder-icon"></i>

                <input ng-model="searchText" type="text" placeholder="Search">
          </label>
        </div>
        <a><div  ng-click="toUserView($index)" value="Taba" ng-repeat="card in cards | filter:searchText" class="list card ">
            <div class="item item-avatar item-icon-right">
                <img ng-src="{{card.photoURL}}">
                <h2>{{card.name}}</h2>

                <!-- *class time / time and icon -->
                                <!-- *class icon-p / icon location -->
                <p class="icon-p">

                    {{card.description}}
                </p>
                <p style="color:white;" id="rotaryId{{$index}}" >{{card.rotaryId}}</p>
            </div>


        </div></a>

    </ion-content>
</ion-view>

2 个答案:

答案 0 :(得分:1)

尝试使用:

 var userId = firebase.auth().currentUser.uid;
 var ref = firebase.database().ref('/accounts/' + userId + "/cards/");

 var cards2 = [];
  ref.orderByChild('cards').on("value", function(snapshot1) {
       var cards1 = snapshot1.val();
       for(var i in cards1){
          cards2.push(cards1[i])
        }
        $scope.cards = cards2;
        console.log(cards2);
      })
    })

在这种情况下,您将获得对象数组

答案 1 :(得分:0)

  1. Firebase实际上并不存储数组,如果可以的话,最好避免使用firebase中的数组:https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html

  2. 为什么不迭代对象的键? How can I iterate over the keys, value in ng-repeat in angular

  3. 变化:

    ng-repeat="card in cards
    

    为:

    ng-repeat="(key, card) in cards
    

    例如:

    <a><div  ng-click="toUserView($index)" value="Taba" ng-repeat="(key, card) in cards | filter:searchText" class="list card ">
        <div class="item item-avatar item-icon-right">
            <img ng-src="{{card.photoURL}}">
            <h2>{{card.name}}</h2>
    
            <!-- *class time / time and icon -->
                            <!-- *class icon-p / icon location -->
            <p class="icon-p">
    
                {{card.description}}
            </p>
            <p style="color:white;" id="rotaryId{{$index}}" >{{card.rotaryId}}</p>
        </div>
    
    
    </div></a>