无法访问Firebase对象属性。值显示为未定义的ANGULARFIRE [英] Cannot access Firebase object attributes. Value displayed as undefined ANGULARFIRE

查看:109
本文介绍了无法访问Firebase对象属性。值显示为未定义的ANGULARFIRE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用用户名访问Firebase内部的引用网址,但是我遇到了一个问题。

我得到一个返回的对象。如果我想访问一个属性,我得到未定义的值,但是如果我添加在我的html {{returnedObj.name}} 中,则会显示该值。



这是为什么?

  angular.module('Demo')。controller ('loginCtrl',['$ scope','$ firebase','$ location',function($ scope,$ firebase,$ location){
$ scope.user = {};
$ scope.check = function(){
console.log('https://fabritzio-demo.firebaseio.com/users/'+ $ scope.user.name);
$ scope.returnedObj = $ firebase(新的Firebase('https://fabritzio-demo.firebaseio.com/usuarios/'+ $ scope.user.name))。$ asObject();
alert($ scope.returnedObj.name) ; //返回undefined值
};

}]);

解决方案

Firebase值是异步加载的。当警报触发时,该值尚未加载到 $ scope.returnedObj 中。



有两种方法可以处理从Firebase异步加载的值,例如使用 $ loaded 来获得承诺:


$ b $ $ p $ $ scope.returnedObj。$ loaded()。then(function(){
alert($ scope.returnedObj.name);
});

该值显示在模板中,因为Angular监视所有 $ scope 变量进行更改。当数值加载(毫秒后),立即显示。


I want to do a custom login for a demo of my doing, but I encountered a problem.

I use the username to access a reference url inside Firebase, I get a returned object. If I want to access a single attribute, I get the undefined value, but if I add in my html {{returnedObj.name}} the value is displayed.

Why is that?

angular.module('Demo').controller('loginCtrl', ['$scope', '$firebase', '$location',    function($scope, $firebase, $location){
$scope.user = {};
$scope.check = function(){
    console.log('https://fabritzio-demo.firebaseio.com/users/' + $scope.user.name);
    $scope.returnedObj = $firebase(new Firebase('https://fabritzio-demo.firebaseio.com/usuarios/' + $scope.user.name)).$asObject();
    alert($scope.returnedObj.name); // returns undefined value
};

}]);

解决方案

Firebase values are loaded asynchronously. The value will not yet have been loaded into $scope.returnedObj when the alert fires.

There are a couple of ways to handle values loading asynchronously from Firebase, for example using $loaded to get a promise:

$scope.returnedObj.$loaded().then(function () {
  alert($scope.returnedObj.name);
});

The value is displayed in the template because Angular watches all $scope variables for changes. When the value is loaded (milliseconds later), it is immediately displayed.

这篇关于无法访问Firebase对象属性。值显示为未定义的ANGULARFIRE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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