0
如何訪問我的$ 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' –
否,出於某種原因,在函數運行之後它也是未定義的。 – olivier
向我們展示完整的控制器代碼 –