2017-04-03 26 views
0

我试图使用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中的瓶ID。它工作正常,因为我可以在日志中从maBouteille.js

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); 
}]); 

看到ID我想不通我怎么可以用这个$ routeParams.id检索火力地堡中的相应记录。任何想法?

+0

你会得到完整的对象在bouteille对象ng重复。为什么你想要获取@fabien –

+0

在MaBouteilleCtrl中,我想从点击瓶中找到数据 –

+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。

+0

谢谢Ankit。我可以在日志中看到它!然而,我仍然努力在我的html中显示它... –

+0

我创建了一个函数extractData(test),在console.log下面调用它,但它不起作用。 {{name}}不会检索该名称。任何想法如何终止工作? function extractData(name){ console.log(“Name:”+ name); $ scope.name = name; } –

+0

我找到了! $( '#名称')追加(snapshots.val()名称。);在html中的功能和