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

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

问题描述

我在访问利用火力加载数据的麻烦。$的方法AngularFire 0.5.0

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

在我退出范围的内容的数据是存在的,但是当我尝试使用更深入的数据我得到的回调未定义。也许我误解你如何意味着这种方法来访问数据吗?

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回报未定义

推荐答案

根据<一个href=\"http://stackoverflow.com/questions/20598636/how-can-you-manage-invalid-references-in-angularfire-0-5-0\">Kato's回答这里我已经能够解决这个问题。
我不知道加载的情况下通过加载资产的原始数据,它是无证在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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