无法访问 Firebase 对象属性.值显示为未定义的 ANGULARFIRE [英] Cannot access Firebase object attributes. Value displayed as undefined ANGULARFIRE
问题描述
我想自定义登录来做一个演示,但是遇到了问题.
I want to do a custom login for a demo of my doing, but I encountered a problem.
我使用用户名访问 Firebase 中的引用 url,我得到一个返回的对象.如果我想访问单个属性,我会得到未定义的值,但是如果我在我的 html {{returnedObj.name}}
中添加该值,则会显示该值.
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.
这是为什么?
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 值是异步加载的.当 alert
触发时,该值尚未加载到 $scope.returnedObj
中.
Firebase values are loaded asynchronously. The value will not yet have been loaded into $scope.returnedObj
when the alert
fires.
有几种方法可以处理从 Firebase 异步加载的值,例如使用 $loaded
来获取承诺:
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);
});
该值显示在模板中是因为 Angular 会监视所有 $scope
变量的变化.加载值时(毫秒后),立即显示.
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屋!