2017-04-03 38 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中的功能和