AngularFire - How to define item ID for child temp

2019-03-01 09:59发布

问题:

Maybe my issue seems easy to resolve, but I've this problem since a lot of hours : When I'm in my dashboard, all data of my Firebase database are visible (With Ng-repeat). But I can't found a solution for choose one specific item and see his details in another page.

I've test this method in HTML (This is an example) :

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

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

</div>

In App JS :

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

In Service JS (in Post Factory) :

 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;

}]);

My PostCtrl :

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

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

  $scope.posts = Post.all;

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

  $scope.auth = Auth;

PS : It took 16 hours to try a bunch of tutorials mostly obsolete , and I am sure that the solution can not be that simple.

I already posted two similar issues yesterday and this morning but each of the proposed solutions have not worked . I would be immensely grateful to the person who would help me out of this impasse .

I still have a little trouble with jsFiddle promised I would learn to use it once I would have solved this problem.

Thank you for giving me time

回答1:

Can you please try the following set of codes, I've explained the changes in comments in Controller and added :id in App JS

In App 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;


回答2:

you can use route provider to do that. I used that for my application and it works great.

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