使用AngularFire v2获取关键值的一点点清晰? [英] A little clarity on getting key values using AngularFire v2?

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

问题描述

对不起,这有点疯狂:

假设我想获得一个项目的标题和.priority键加载时的值。



首先让我们看看每个东西的布局:

  $ scope.items = $ firebase(新的Firebase(https://****.firebaseio.com)); 
$ b $ scope.items。$ on('loaded',function(){

console.log($ scope.items);

});

我们得到:

 >对象{$ 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){

>这是我的项目:对象
$$ hashKey:012
$ id:这是我的项目
$ priority:2884707
title:这是我的项目
__proto__:Object


> __proto__:Object

看起来不错,让我们试着抓住$ priority:

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

糟糕:

 未捕获错误:Firebase.child失败:第一个参数是无效路径:$ priority。路径必须是非空字符串,不能包含。,#,$,[或]

好的,我们现在就跳过这个题目,试试这个题目:

 的console.log($ scope.items $子( '标题')); 

>对象{$ 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__:对象

无标题



好吧,让我们以不同的方式尝试:

  var firstItem = $ scope.items。$ getIndex()[0]; //以优先级顺序返回$ ids 
console.log($ scope.items。$ child(firstItem));

>对象{$ 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:这是我物品的标题
> __proto__:Object

但现在没有优先权!

厨房水槽在电脑时代宣誓:

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

> undefined

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

> Uncaught TypeError:无法读取未定义的属性'title'
$ b console.log(Object.keys($ scope.items));

> $$,$ $,$ $,$ $,$ $,$ $,$ $, c>

我错过了什么?我可以设法得到'标题'通过做两次掉头(弄清楚什么项目的$ id是使用'$ getIndex()',抓住第n项,然后使用'$ child()'再次调用),但即使这不符合优先顺序。有没有一种更简单的方法从AngularFire v2中获取关键值?或者任何方式来获得$优先?

解决方案

我相信我/遇到同样的问题。希望这与你的经验有关。即:
$ b

$ scope.items = $ firebase(new Firebase(https:// xxxxxx .firebaseio.com));
$ scope.items $ on('loaded',function(){
console.log($ scope.items ['some key']); //这个写入未定义到控制台
});

我注意到这个确实有效依靠加载的事件。例如在HTML中:
$ b

< a href =#data-ng-click = logItem()>点击我< / a>

和在JS中:

  $ scope.items = $ firebase(新的Firebase(https://xxxxxx.firebaseio.com)); 
$ scope.logItem = function(){
console.log($ scope.items ['some key']); / /这工程

点击点击我链接记录我的项目控制台如预期。



感觉像加载时间的集合没有完全填充来自firebase的基础项目。


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

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);

});

With that we get:

> 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

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

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

Oops:

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

No title.

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

But now there's no priority!

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"]

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
});

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>

and in the 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.

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

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

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