【发布时间】:2016-08-01 00:25:12
【问题描述】:
如何在这个函数之外访问我的 $scope.key?
.controller('CatsCtrl', function($scope, $state, $http) {
var query = firebase.database().ref().orderByKey();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
$scope.key = childSnapshot.key;
// I need this value
$scope.childData = childSnapshot.val();
$scope.data = $scope.childData.Category;
console.log($scope.key);
});
});
console.log($scope.key);
// returns undefined
var ref = firebase.database().ref($scope.key);
ref.once("value")
.then(function(snapshot) {
console.log(snapshot.val());
var name = snapshot.child("Category").val();
console.log(name);
});
})
我已经用 $scope.apply 试过了,但这没有用。
【问题讨论】:
-
函数运行后可以访问
$scope.key -
不,由于某种原因,在函数运行后它也未定义。
-
向我们展示完整的控制器代码
-
更新了问题...
-
你运行的函数是异步的,它会在你的console.log语句之后运行
标签: angularjs ionic-framework firebase firebase-realtime-database