我是AngularJS的新手,我想在这里推送数据是我的代码:
$scope.cards = [];
firebase.auth().onAuthStateChanged(function(user) {
var firebaseRef = firebase.database().ref("users_location");
var geoFire = new GeoFire(firebaseRef);
var posOptions = {timeout: 3000, enableHighAccuracy: false};
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (position) {
$scope.cards = [];
var geoQuery = geoFire.query({
center: [position.coords.latitude, position.coords.longitude],
radius: 10.5
});
var onKeyEnteredRegistration = geoQuery.on("key_entered", function(key, location, distance) {
firebase.database().ref('users/'+key).on('value', function(snapshot) {
$scope.cards.push({
id: key,
image: 'img/adam.jpg',
about: "Test message"
});
});
});
console.log($scope.cards); // <--- show "[]"
});
});
为什么console.log($scope.cards);
告诉我“[]”,如果我将此代码放入firebase函数中,我有数据?
谢谢
答案 0 :(得分:0)
这是因为函数的异步行为和由于
console.log($scope.cards);
在
结果之前调用 firebase.database()
答案 1 :(得分:0)
我认为console.log($scope.cards);
在$scope.cards
加载数据之前运行。
在角度js console.log($scope.cards);
中添加$scope.$watch
以查看数据。
如果您想在$scope.cards
加载数据后运行任何代码,请在$scope.$watch
中添加该代码。