循环通过 $id 从 Firebase 后端生成定制的唯一页面数据

Looping through $id to generate customized unique page data from Firebase backend

我正在 AngularJS 和 Firebase 中创建博客。 我 运行 遇到的问题是,当用户单击 link 到特定博客 post 时,我试图成功呈现唯一的页面数据,该博客被写入后端的 firebase。

我的 firebase 数据在 "https:ngblogapp.firebaseio.com/posts"

这是我的图书馆的视觉效果

我正在使用 ng-repeat="post in posts" 呈现页面 links,其中 posts 指的是 firebase 库。 这是一个视觉效果。

当我点击其中一个 link 时,我进入了一个具有唯一 url id 的页面 ''' http://localhost:7000/#/post/-K0T3PqoCBVygt_LCCHF ''' but the unique page displays the data as such

让我带你进入我的javascript。

var app=angular.module('myBlogApp',
[
'ngRoute',
'app.controllers',
'app.directives',
'firebase'
])
.constant('FBURL', "https://ngblogapp.firebaseio.com/posts") //fburl
.config(['$routeProvider', function($routeProvider){
    $routeProvider.when('/',{
        templateUrl: '../views/posts.html',
        controller: 'postController'
    }).when('/post/:postId',{
        templateUrl:'../views/singlepost.html',
        controller: 'postController'
    }).when('/page/:postId',{
        templateUrl:'../views/page.html',
        controller: 'postController' //'PageController'
    }).when('/create',{
        templateUrl:'../views/createaPost.html',
        controller:'postController'
    }).otherwise({
      redirectTo: '/'
    });
}
]);
app.factory("Blog",["$firebaseArray","$routeParams",   function($firebaseArray, $routeParams){

var ref = new Firebase("https://ngblogapp.firebaseio.com/posts");//  FIREBASE OBJ  
var blogPostsArray = $firebaseArray(ref);
  return{

    id: $routeParams.postId,

    allPosts: blogPostsArray, // all fb objects

    addPost: function(newpost){
      newpost.datetime = Firebase.ServerValue.TIMESTAMP;
      return blogPostsArray.$add(newpost); //push to array
    }

};

}]);



app.controller('postController',["$scope",     "$location","$routeParams","Blog","FBURL", "$firebaseObject"    ,function($scope,$location,$routeParams,Blog,FBURL,$firebaseObject){

$scope.posts = Blog.allPosts; //All blog posts
var postId = $routeParams.postId;

if(postId){
$scope.selectedPost = getPost(postId);
}

function getPost(postId){
var ref = new Firebase(FBURL + postId);
return $firebaseObject(ref);
}

$scope.addPost = function(newpost){
Blog.addPost($scope.newpost);
//$location.path('/'); //redirects to home page
console.log(newpost);
console.log($scope.posts); // all posts
$scope.newpost ={}; //reset the message
};

$scope.currentPost = function(postId){
Blog.getPost(postId);
console.log(postId);
};

}]);

在 'postController' 中,我有一个名为 getPost(postId) 的方法,它接受一个参数 postId。 我的 postId 被分配给一个 routeParam 就像这样 postId = $routeParams.id.

在我的 html 部分中,我正在遍历 posts.$id 像这样:

<div class="container">
<div class="col-md-offset-9 col-md-3">
<!--span(class="glyphicon glyphicon-search" aria-hidden="true")-->
<input type="text" ng-model="searchPost" placeholder="Search"  class="form-control"/>
</div>
<article ng-repeat="post in posts | filter: searchPost | orderBy:  date:'medium' ">
<h1 class="page-header"><a ng-href="#/post/{{post.$id}}">{{post.title}}
    <h3>{{post.text | limitTo: 30}}{{post.text.length > 30? "...": ""}}         <i>{{post.datetime | date:short}}</i></h3></a></h1>
</article>
</div>

同样,这会在 url 上呈现 id,但会将我引导至一个时髦的对象

{"$id":"posts-K0T3PqoCBVygt_LCCHF","$priority":null,"$value":null}

我不知道什么是正确的 API 调用来获取对象的唯一 ID 并使用 AngularFire 显示其内容。任何建议都会很棒。

我认为你只是少了一个斜杠

var postId = $routeParams.postId;

基于https://docs.angularjs.org/api/ngRoute/service/$routeParams 它returns一个没有斜杠的参数值的对象

function getPost(postId){
    // FBURL + postId = ...firebaseio.com/posts-K0T3PqoCBVygt_LCCHF
    // FBURL + '/' + postId = ...firebaseio.com/posts/-K0T3PqoCBVygt_LCCHF
    var ref = new Firebase(FBURL + postId);
    return $firebaseObject(ref);
}

恕我直言,修复是 new Firebase(FBURL + '/' + postId),但我无法测试(或重现)。