本文介绍了使用 AngularFire v2 获取关键值有一点清晰吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉这么长,但它让我有点发疯:

Sorry for the length of this but it's driving me a little bit crazy:

假设我想在加载时获取项目的标题"和.priority"键值.

Let's say I want to get an item's "title" and ".priority" key values when it loads.

首先让我们看看所有东西的布局:

First let's see how every thing's laid out:

 $scope.items = $firebase(new Firebase("https://****.firebaseio.com"));

 $scope.items.$on('loaded', function() { 

console.log($scope.items);

});

我们得到:

> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
    > $add: function (item, cb) {
    > $bind: function (scope, name) {
    > $child: function (key) {
    > $getIndex: function () {
    > $on: function (type, callback) {
    > $remove: function (key) {
    > $save: function (key) {
    > $set: function (newValue) {

    > this is my item: Object
          $$hashKey: "012"
          $id: "this is my item"
          $priority: 2884707
          title: "this is the title of my item"
          __proto__: Object


    > __proto__: Object

看起来不错,让我们尝试获取 $priority:

Looks good, let's try to grab that $priority:

 console.log($scope.items.$child('$priority'));

糟糕:

Uncaught Error: Firebase.child failed: First argument was an invalid path: "$priority". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"

好的,我们暂时跳过它,直接尝试标题:

Fine, we'll skip that for now and just try for the title:

console.log($scope.items.$child('title'));

> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
    > $add: function (item, cb) {
    > $bind: function (scope, name) {
    > $child: function (key) {
    > $getIndex: function () {
    > $on: function (type, callback) {
    > $remove: function (key) {
    > $save: function (key) {
    > $set: function (newValue) {
    > __proto__: Object

无标题.

好的,让我们换一种方式试试:

Okay, let's try it a different way :

    var firstItem = $scope.items.$getIndex()[0];  //returns $ids in order of priority
     console.log($scope.items.$child(firstItem));

     > Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
         > $add: function (item, cb) {
         > $bind: function (scope, name) {
         > $child: function (key) {
         > $getIndex: function () {
         > $on: function (type, callback) {
         > $remove: function (key) {
         > $save: function (key) {
         > $set: function (newValue) {
              title: "this is the title of my item"
         > __proto__: Object

但现在没有优先级了!

厨房水槽对着电脑发誓:

Kitchen sink swearing at the computer time:

 console.log($scope.items.title);

     > undefined

 console.log($scope.items[0].title);

     > Uncaught TypeError: Cannot read property 'title' of undefined 

  console.log(Object.keys($scope.items));     

     > ["$bind", "$add", "$save", "$set", "$remove", "$child", "$on", "$getIndex"]

我错过了什么?我可以通过做几次掉头来设法获得标题",(使用$getIndex()"找出项目的 $id 是什么,抓取第 n 个项目,然后使用$child()"进行另一个调用),但即使这样也不适用于 $priority.有没有更简单的方法可以从 AngularFire v2 中获取键值?或者有什么方法可以获得 $priority?

What am I missing? I can manage to get the 'title' by doing a couple U-turns, (figuring out what the item's $id is using '$getIndex()', grabbing the nth item and then making another call using '$child()' ), but even that doesn't work for $priority. Is there an easier way to get key values from within AngularFire v2? Or any way at all to get the $priority?

推荐答案

我相信我正在/正在遇到同样的问题.希望这与您的经历有关.即:

I believe I am / was experiencing the same issue. Hopefully this is related to your experience. Namely:

$scope.items = $firebase(new Firebase("https://xxxxxx.firebaseio.com"));
$scope.items.$on('loaded', function() {
  console.log($scope.items['some key']); // this writes undefined to console
});

我注意到,如果我不依赖 loaded 事件,这实际上 确实有效.例如在 HTML 中:

What I noticed is that this actually does work if I don't rely on the loaded event. E.g in the HTML:

<a href="#" data-ng-click="logItem()">click me</a>

在 JS 中:

$scope.items = $firebase(new Firebase("https://xxxxxx.firebaseio.com"));
$scope.logItem = function() {
  console.log($scope.items['some key']); // this works
}

单击单击我"链接将我的项目按预期记录到控制台.

Clicking on the "click me" link logs my item to the console as expected.

感觉 loaded 时的集合没有完全填充来自 firebase 的基础项目.

Feels like the collection at loaded time isn't fully populated with the underlying items from firebase.

这篇关于使用 AngularFire v2 获取关键值有一点清晰吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:47