2016-07-24 93 views
0

我正在开发我的AngularFire项目。我有一个像这样的数据库https://dinosaur-facts.firebaseio.com/。我正在做的是首先搜索像恐龙孩子这样的关键字,比如“linhenykus”,并将其键和值存储到其他$ scope变量中。
我在控制器AngularFire搜索关键节点并分配给变量

var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs" + "/" + $scope.dinoID); //$scope.dinoID is variable containg dino keys 

$scope.detRef = $firebaseArray(ref); 

我越来越喜欢

[{"$value":-85000000,"$id":"appeared","$priority":null},{"$value":0.6,"$id":"height","$priority":null},{"$value":1,"$id":"length","$priority":null},{"$value":"theropoda","$id":"order","$priority":null},{"$value":-75000000,"$id":"vanished","$priority":null},{"$value":3,"$id":"weight","$priority":null}] 

输出如何获取键和值码?

+0

你是什么意思的*键和值*?我只是看到一个'数组'对象'的属性.. – developer033

+0

正确的对象。如何获取这些对象? – shubgpt

+0

在您看来您的意思是? – developer033

回答

1

使用ngRepeat指令,如下:

<tr ng-repeat="obj in array track by $index"> 

要过滤阵列可以使用filterAngular的:

<tr ng-repeat="obj in array | filter: criteria track by $index"> 

这里是一个例如

(function() { 
 
    angular 
 
    .module('app', []) 
 
    .controller('MainCtrl', MainCtrl); 
 

 
    MainCtrl.$inject = ['$scope', '$filter']; 
 

 
    function MainCtrl($scope, $filter) { 
 
    $scope.criteria = {}; 
 
    $scope.array = [ 
 
     { 
 
      "$value":-85000000, 
 
      "$id":"appeared", 
 
      "$priority":null 
 
     }, 
 
     { 
 
      "$value":0.6, 
 
      "$id":"height", 
 
      "$priority":null 
 
     }, 
 
     { 
 
      "$value":1, 
 
      "$id":"length", 
 
      "$priority":null 
 
     }, 
 
     { 
 
      "$value":"theropoda", 
 
      "$id":"order", 
 
      "$priority":null 
 
     }, 
 
     { 
 
      "$value":-75000000, 
 
      "$id":"vanished", 
 
      "$priority":null 
 
     }, 
 
     { 
 
      "$value":3, 
 
      "$id":"weight", 
 
      "$priority":null 
 
     } 
 
    ]; 
 

 
    $scope.getObj = function(value, id) { 
 
     $scope.obj = $filter('filter')($scope.array, { 
 
     "$value": value, 
 
     "$id": id 
 
     })[0]; 
 
    } 
 

 
    $scope.getObj("theropoda", "order"); 
 
    } 
 
})();
table, th, td { 
 
    border: 1px solid black; 
 
    border-collapse: collapse; 
 
}
<!DOCTYPE html> 
 
<html ng-app="app"> 
 

 
<head> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 
 
</head> 
 

 
<body ng-controller="MainCtrl"> 
 
    <input type="text" placeholder="Search by value" ng-model="criteria.$value"> 
 
    <input type="text" placeholder="Search by id" ng-model="criteria.$id"> 
 
    <!-- Note that this button is just as an example to change the object displayed below --> 
 
    <button type="button" ng-click="getObj()">Change the value of object</button> 
 
    <hr> 
 
    Obj: <span ng-bind-template="Value: {{obj.$value}}, Id: {{obj.$id}}"></span> 
 
    <p></p> 
 
    <table width="100%"> 
 
    <thead> 
 
     <tr> 
 
     <th>Value</th> 
 
     <th>Id</th> 
 
     <th>Priority</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <tr ng-repeat="obj in array | filter: criteria track by $index"> 
 
     <td ng-bind="obj.$value"></td> 
 
     <td ng-bind="obj.$id"></td> 
 
     <td ng-bind="obj.$priority"></td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
</body> 
 

 
</html>

+0

谢谢!你能告诉我如何在视图中显示特定的ID和价值吗? – shubgpt

+0

基于过滤器? – developer033

+0

就像我只想展示theropoda并订购 – shubgpt