Tg7*_*g7z 4 angularjs firebase angularfire
我无法使用AngularFire 0.5.0中的.$ on方法访问从firebase加载的数据
在回调当我注销范围的内容时,数据就在那里,但当我尝试使用更深入的数据时,我得到了undefined.也许我误解了你如何访问这种方法中的数据?
这是我的控制器:
.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
});
}])
Run Code Online (Sandbox Code Playgroud)
所以也许有更好的方法来做到这一点.
为什么即使它登录到控制台,我也无法访问范围内的数据?
这是第一个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
Run Code Online (Sandbox Code Playgroud)
第二个console.log返回 undefined
基于加藤的答案,我已经能够解决这个问题.
我不知道加载的事件传递了已加载资产的原始数据,它在AngularFire文档中没有记录.
它没有解释我使用console.log的奇怪行为,但它确实解决了这个问题.
.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
});
}])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2047 次 |
| 最近记录: |