Fab*_*gas 4 angularjs 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(new Firebase('https://fabritzio-demo.firebaseio.com/usuarios/' + $scope.user.name)).$asObject();
alert($scope.returnedObj.name); // returns undefined value
};
Run Code Online (Sandbox Code Playgroud)
}]);
aft*_*oon 12
Firebase值以异步方式加载.火灾发生$scope.returnedObj时,该值尚未加载alert.
有几种方法可以处理从Firebase异步加载的值,例如使用$loaded获取承诺:
$scope.returnedObj.$loaded().then(function () {
alert($scope.returnedObj.name);
});
Run Code Online (Sandbox Code Playgroud)
该值显示在模板中,因为Angular会监视所有$scope变量以进行更改.加载值(毫秒后),立即显示.
| 归档时间: |
|
| 查看次数: |
3776 次 |
| 最近记录: |