我需要从$ scope到其他的数据库的不同id获取最大值

时间:2016-12-13 16:17:13

标签: javascript angularjs firebase firebase-realtime-database

所以我有这个数据库:

enter image description here

我需要对这些子ID进行排序(从type1type10),在UI处显示排序值,id的最大值将是我想要放置的新变量$id(例如,在这种情况下,最大值为84.5,其中$id名为type2)为sifat="type2"

我应该在一个新变量中声明它们然后对它进行排序吗?

2 个答案:

答案 0 :(得分:1)

您必须在客户端上进行排序,但您可以使用angularfires $firebaseArray将所有内容放入数组中:

$firebaseArray(...child('result')).$loaded().then(function(types){
    $scope.types = types;
})

然后在dom中排序(或者你需要的任何地方)

<div ng-repeat="type in types | orderBy:'value'"></div>

https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray

答案 1 :(得分:0)

我解决了这个问题

 fireBaseData.refUser().child($scope.user_info.uid).child("result")
                  .orderByValue()
                  .limitToLast(1)
                  .on("value", function(snapshot){
                     snapshot.forEach(function(data){
                       console.log("The "+ data.key()+" score is "+ data.val());
                       $scope.highestval = data.val();
                       $scope.highesttype = data.key();

                       fireBaseData.refUser().child($scope.user_info.uid).update({    // set
                         sifat: $scope.highesttype
                       });
                     });
                  }

并且@Mathew Berg的过滤功能也正常,谢谢