加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何从AngularFire 0.5.0中的.$on(‘loaded’)访问

发布时间:2020-12-17 17:48:48 所属栏目:安全 来源:网络整理
导读:我无法使用AngularFire 0.5.0中的.$on方法访问从firebase加载的数据 在回调当我注销范围的内容时,数据就在那里,但是当我尝试使用更深层次的数据时,我得到了未定义.也许我误解了你如何访问这种方法中的数据? 这是我的控制器: .controller('AssetDetailCtrl'
我无法使用AngularFire 0.5.0中的.$on方法访问从firebase加载的数据

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

这是我的控制器:

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

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

这是第一个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

解决方法

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

它没有解释我使用console.log的奇怪行为,但它确实解决了这个问题.

.controller('AssetDetailCtrl',function(value) {
      console.log(value); // data loaded from Firebase
      console.log(value.name); // subset of the returned value
    });
}])

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读