2015-07-10 45 views
1

也許我的問題似乎很容易解決,但自從很長時間以來,我遇到了這個問題:當我在儀表板中時,我的Firebase數據庫的所有數據都可見(使用Ng-repeat)。 但我找不到解決方案來選擇一個特定的項目,並在另一個頁面中查看他的詳細信息。AngularFire - 如何使用ng-repeat爲子模板定義項目ID?

我已經測試在HTML這種方法(這是一個例子):

<div ng-repeat="post in posts"> 

<div class="card" ng-href="#/post/">"> 
<h1>{{post.title}}</h1> 
<p>{{post.content}}</p> 
</div> 

</div> 

在應用JS:

.state('tabpost', { 
    url: 'tabpost/id', 
    templateUrl: 'templates/tab-post.html', 
    controller: 'PostCtrl' 
    }) 

在服務JS(在後廠):

myApp.factory("Post", ["$firebaseArray", "$firebaseObject", function($firebaseArray, $firebaseObject) { 
var postRef = new Firebase('https://myApp.firebaseio.com/Posts/'); 
var userRef = new Firebase('https://myApp.firebaseio.com/Users/'); 
var posts = $firebaseArray(postRef); 

    var Post = { 

     all: posts, 

     get: function (postKey){ 
      var postId = $firebaseObject(postRef); 
       return $firebaseObject(eventRef.child('Posts').child(postId).child(userid)); 

       } 
      , 
     add: function (post){ 
      var postId = $firebaseArray(postRef, userRef); 
      event.userid = userRef.getAuth(); 
       return postId.$add(post); 
       } 
     } 
     return Post; 

}]); 
我的郵箱:

PS:花了16個小時來嘗試一大堆已經過時的教程,我相信解決方案不會那麼簡單。

我昨天和今天早上已經發布了兩個類似的問題,但每個建議的解決方案都沒有奏效。我將非常感謝能幫助我走出僵局的人。

我還是有點麻煩jsFiddle答應我會學會使用它,一旦我會解決這個問題。

謝謝你給我時間

+0

它應該是'url:'tabpost /:id''? –

+0

你得到的錯誤是什麼? –

+0

這是糾正,但我有同樣的結果:不工作... – PositivProd

回答

0

能否請您嘗試以下代碼集,我已經在控制器的意見解釋的變化和應用程序添加:id JS

在應用JS:

.state('tabpost', { 
    url: 'tabpost/:id', 
    templateUrl: 'templates/tab-post.html', 
    controller: 'PostCtrl' 
    }) 

PostCtrl:

myApp.controller('PostCtrl', ['$ionicFrostedDelegate', '$ionicScrollDelegate','$state','$scope', 'Post', 'Auth', '$firebaseObject', '$firebaseArray', '$routeParams', function($ionicFrostedDelegate, $ionicScrollDelegate, $state,$scope, Post, Auth, $firebaseObject, $firebaseArray, $routeParams) { 

    var PostRef = new Firebase("https://myApp.firebaseio.com/Posts"); 

    var id = $routeParams.id; //get the id from url, $routeParams is injected in controller 

    $scope.posts = Post.get(id); //call get() method of Post with the id retrieved 

    $scope.post = {'title': '', 'content': ''}; 

    $scope.auth = Auth; 
+0

我有一個錯誤消息:錯誤:無法解析'postId:post.id'狀態'tabdash',謝謝爲你的時間 – PositivProd

+0

嗯..猜猜需要更多的服務代碼。你可以發佈嗎? –

+0

這樣做你可以看到它! ;) – PositivProd

0

你可以使用路由提供者來做到這一點。我用它來處理我的應用程序,效果很好。

myApp.config(['$routeProvider', function($routeProvider){ 
    $routeProvider. 
     when('tabpost', { 
      templateUrl: 'tabpost/id', 
      controller: 'PostCtrl' 
     }); 
}]); 
+0

在應用JS或分離控制器?感謝您的時間.. – PositivProd

+0

@ YngDev31這應該是在appjs –

+0

好吧,所有帖子顯示的頁面被命名爲tabdash,而單個帖子頁面是tabpost。有了這個,這總是正確的,或者我必須改變你的片段?謝謝@Coding Enthusiast – PositivProd

相關問題