Firebase通过$ routeParams中的$ id检索子项

时间:2017-04-03 06:48:50

标签: angularjs firebase firebase-realtime-database

我正在尝试使用该URL将信息从我的列表传递到详细信息视图。我的网址包含Firebase ID。我确实设法检索了ID,但我找不到如何阅读Firebase中的相​​应记录。

我有一个瓶子清单,当我点击一个瓶子时,我想显示它的名字。

bouteilles.js

angular.module('myApp.bouteilles', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/bouteilles', {
    templateUrl: 'bouteilles/bouteilles.html',
    controller: 'BouteillesCtrl'
  });
}])

.controller('BouteillesCtrl', ['$scope','$firebaseArray', function($scope, $firebaseArray) {

  var ref = firebase.database().ref();
  $scope.bouteilles = $firebaseArray(ref);

}]);

然后我有一份" bouteilles" : bouteilles.html

<div class = "list" ng-repeat="bouteille in bouteilles>
  <a class="item item-avatar" href="#!/maBouteille/{{bouteille.$id}}">
    <h2>{{bouteille.name}} - {{bouteille.year}}</h2>
  </a>
</div>

因此我在URL中有瓶号。它工作正常,因为我可以在日志中看到 maBouteille.js

中的id
angular.module('myApp.maBouteille', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/maBouteille/:id', {
    templateUrl: 'maBouteille/maBouteille.html',
    controller: 'MaBouteilleCtrl'
    });
}])

.controller('MaBouteilleCtrl', ['$routeParams','$scope','$firebaseArray', function($routeParams, $scope, $firebaseArray) {
  var ref = firebase.database().ref();
  console.log($routeParams.id);   
}]);

我无法弄清楚如何使用此$ routeParams.id来检索Firebase中的相​​应记录。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

.controller('MaBouteilleCtrl', ['$routeParams','$scope','$firebaseArray',     function($routeParams, $scope, $firebaseArray) {
     var ref = firebase.database().ref('your Node or table name'); //that node ref
         ref.child($routeParams.id).on("value",function(snapshots){ //value of that id
         console.log(snapshots.val()); //printing object
      })
  console.log($routeParams.id);   
}]);

这里我使用角度js和firebase。

相关问题