你如何从 AngularFire 0.5.0 中的 .$on('loaded') 访问加载的数据 [英] How do you access loaded data from .$on('loaded') in AngularFire 0.5.0

查看:15
本文介绍了你如何从 AngularFire 0.5.0 中的 .$on('loaded') 访问加载的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 AngularFire 0.5.0 中的 .$on 方法访问从 firebase 加载的数据时遇到问题

I am having trouble accessing the data loaded from firebase using the .$on method in AngularFire 0.5.0

在我注销范围内容时的回调中,数据在那里,但是当我尝试使用更深层次的数据时,我得到 undefined.也许我误解了您打算如何以这种方法访问数据?

In the callback when I log out the contents of the scope the data is there but when I try to use deeper data I get undefined. Perhaps I am misunderstanding how you are meant to access data in this method?

这是我的控制器:

.controller('AssetDetailCtrl',
  ['$scope', '$firebase', 'FBURL',
  function($scope, $firebase, FBURL) {
    var refAsset = new Firebase(FBURL + '/assets/' + $scope.assetId);
    $scope.asset = $firebase(refAsset);

    // when data is loaded check validity of the route
    $scope.asset.$on('loaded', function() {
      console.log($scope.asset); // complete with the asset data
      console.log($scope.asset.name); // undefined even though it appears in the above console log
    });
}])

所以也许有更好的方法来做到这一点.
为什么我无法从范围访问数据,即使它记录到控制台?

So perhaps there is a better way to do this.
Why is it I cannot access the data from the scope the even though it logs to the console?

这是第一个console.log的结果

This is the result of the first console.log

Object { $bind: function, $add: function, $save: function, $set: function, $remove: function…}
  $add: function (b,c){var d;return d="object"==typeof b?a._fRef.ref().push(a._parseObject(b),c):a._fRef.ref().push(b,c)}
  $bind: function (b,c){return a._bind(b,c)}
  $child: function (b){var c=new AngularFire(a._q,a._parse,a._timeout,a._fRef.ref().child(b));return c.construct()}
  $getIndex: function (){return angular.copy(a._index)}
  $on: function (b,c){switch(b){case"change":a._onChange.push(c);break;case"loaded":a._onLoaded.push(c);break;default:throw new Error("Invalid event type "+b+" specified")}}
  $remove: function (b){b?a._fRef.ref().child(b).remove():a._fRef.ref().remove()}
  $save: function (b){b?a._fRef.ref().child(b).set(a._parseObject(a._object[b])):a._fRef.ref().set(a._parseObject(a._object))}
  $set: function (b){a._fRef.ref().set(b)}
  asset_author: Object
  collections: Array[2]
  creator: "John Doe"
  desc: "a description of the asset"
  file: "http://lorempixel.com/400/200/sports/3/"
  filesize: "28kb"
  filetype: "jpg"
  name: "Cycling"
  release: "12/12/2013"
  tags: "tag1, tag3"
  type: "Photography"
  __proto__: Object

第二个 console.log 返回 undefined

Second console.log returns undefined

推荐答案

基于 Kato 在这里的回答 我已经能够解决这个问题.
我不知道加载的事件传递了加载资产的原始数据,它在 AngularFire 文档中没有记录.

Based on Kato's answer here I have been able to solve this.
I was unaware the loaded event passed the raw data of the loaded asset, it is undocumented in the AngularFire docs.

它不能解释我在使用 console.log 时的奇怪行为,但它确实解决了问题.

It doesn't explain the odd behaviour I was having with console.log but it does solve the problem.

.controller('AssetDetailCtrl',
  ['$scope', '$firebase', 'FBURL',
  function($scope, $firebase, FBURL) {
    var refAsset = new Firebase(FBURL + '/assets/' + $scope.assetId);
    $scope.asset = $firebase(refAsset);

    // when data is loaded check validity of the route
    $scope.asset.$on('loaded', function(value) {
      console.log(value); // data loaded from Firebase
      console.log(value.name); // subset of the returned value
    });
}])

这篇关于你如何从 AngularFire 0.5.0 中的 .$on('loaded') 访问加载的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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