在angularfire 1.0.0获取项目通过独特的火力ID [英] Fetching item by unique firebase id in angularfire 1.0.0

查看:165
本文介绍了在angularfire 1.0.0获取项目通过独特的火力ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦使用angularfire 1.0.0我的火力点取一个唯一的项目。为了澄清,我要赋予一个唯一的火力点ID如我的应用程序来获取一个职位-JkZwz-tyYoRLoRqlI_I。它可以在应用程序导航时,例如点击一个链接到一个特定的职位上,但不能在刷新。我的猜测是,它已经是与同步。现在,它获取的所有帖子,并在NG-重复使用它的时候的作品。这是一个线索,为什么它导航到该页面时,适用于一个项目。这也许应该不会很难,因为这应该是pretty规范操作,但我不能得到它的工作。我已经找遍但实际上没有指导这一点。在API它们指的是 $ getRecord(键)

I have trouble fetching one unique item from my firebase using angularfire 1.0.0. To clarify, I want my app to fetch a post given a unique firebase id e.g. "-JkZwz-tyYoRLoRqlI_I". It works when navigating in the app e.g. clicking on a link to a specific post, but not on a refresh. My guess is that it has something to do with synchronization. Right now it works when fetching all posts and use it in a ng-repeat. This is a clue to why it works for one item when navigating to the page. This should probably not be hard since this should be a pretty standard operation, but i can't get it to work. I have searched everywhere but there is actually no guide on this. In the API they refer to $getRecord(key)

返回从数组中给定键的记录。如果该键不
  找到,返回null。该方法利用$ indexFor(键)来查找
  相应的记录。

Returns the record from the array for the given key. If the key is not found, returns null. This method utilizes $indexFor(key) to find the appropriate record.

但正如预期这是行不通的。还是我失去了一些东西?

But this is not working as expected. Or am i missing something?

它适用于这样的NG-重复:

It works for ng-repeat like this:

<div ng-repeat="postt in posts">
   <div>
      <h1>{{postt.title}}</h1>
      <div>{{postt.timestamp}}</div>
      <div>{{postt.content}}</div>
   </div>
</div>

但不是像这个独特的项目:

But not for unique items like this:

<div>
   <h1>{{post.title}}</h1>
   <div>{{post.timestamp}}</div>
   <div>{{post.content}}</div>
</div>

这是该服务:

'use strict';

angular.module('app.module.blog.post')

.factory("PostService", ["$firebaseArray", "FIREBASE_URL", function($firebaseArray, FIREBASE_URL) {

    var ref = new Firebase(FIREBASE_URL + "posts");
    var posts = $firebaseArray(ref);

    return {
        all: posts, // ng-repeat on this works fine

        last: function(nr) {
            var query = ref.orderByChild("timestamp").limitToLast(nr);
            return $firebaseArray(query); // ng-repeat on this work fine to
        },
        create: function (post) {
            return posts.$add(post);
        },
        get: function (postId) {
            console.log(postId); // This is -JkZwz-tyYoRLoRqlI_I
            var post = posts.$getRecord(postId);
            console.log(post); // This print null
            return post;
        },
        delete: function (post) {
            return posts.$remove(post);
        }
    };
}]);

由于意见中get函数说的,是帖子ID那里的帖子也被设置,但后为null。

As the comments say in the get function, the postId is there and posts is also set, but the post is null.

这是控制器

'use strict';

angular.module('app.module.blog.post', [])

.controller('PostCtrl', ['$scope', '$routeParams', 'PostService', function($scope, $routeParams, PostService) {

    // This returns e.g. postId "-JkZwz-tyYoRLoRqlI_I"
    console.log($routeParams.postId);

    $scope.post = PostService.get($routeParams.postId);    
    $scope.posts = PostService.all; // Illustrates the example not actually in this controller otherwise

}]);

这是什么是对什么是在火力数据库为例

This is what is an example on what is in the firebase database

<myfirebase>
 posts
 -JkUnVsGnCqbAxbMailo
 comments
 content: ...
 timestamp: ...
 title: ...
 -JkZwz-tyYoRLoRqlI_I
 comments
 content: ...
 timestamp: ...
 title: ...
 -JkhaEf9tQy06cOF03Ts
 content: ...
 timestamp: ...
 title: ...

我觉得这个问题很奇怪,因为它应该是非常标准的。我显然失去了一些东西,但不能工作了。任何帮助是非常AP preciated!

I find this problem very wierd since it should be very standard. I am obviously missing something, but can't work it out. Any help is very much appreciated!

在此先感谢!

推荐答案

我知道的<一个文档href=\"https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray-getrecordkey\"相对=nofollow> $ getRecord()函数是种误导。你实际上是从 $ firebaseArray 得到的是一个的承诺数组。这意味着,你的帖子变量将包含您的帖子在未来的某一时刻。话虽这么说,看来当阵列已经从火力地堡下载时的承诺已经解决了 $ getRecord 函数仅适用,即。为了确保当你调用 $ getRecord 功能的承诺得到解决,您可以使用 $加载()上承诺:

I know that the documentation of the $getRecord() function is kind of misleading. What you actually get from $firebaseArray is a promise of an array. It means that your posts variable will contain your posts at some point in the future. That being said, it seems that the $getRecord function only works when the promise have been resolved, i.e. when the array has been downloaded from Firebase. To make sure that the promise is resolved when you call the $getRecord function, you can use $loaded() on the promise :

var posts = $firebaseArray(ref);
posts.$loaded().then(function(x) {
    var post = x.$getRecord(postId);
    console.log(post);
}).catch(function(error) {
    console.log("Error:", error);
});

如果你想知道为什么它的工作原理为 NG-重复,这是因为角知道,帖子变量一个承诺,并为它等待渲染值之前得到解决。

If you are wondering why it works for ng-repeat, it's because Angular knows that the posts variable is a promise and waits for it to be resolved before rendering the values.

这篇关于在angularfire 1.0.0获取项目通过独特的火力ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆