I am new in AngularJS and I wanted to know how I can access my $scope
variable from outside function inside same controller. How to achieve this? Here is my code:
.controller('RekapCtrl', ['$scope', '$timeout', '$state', '$stateParams', '$firebaseArray', 'Alkitabdetail', function($scope, $timeout, $state, $stateParams, $firebaseArray, Alkitabdetail) {
var rootRef = new Firebase('https://ayobacaalkitab.firebaseio.com/');
var childUsers = rootRef.child('users');
var childDate = rootRef.child('tanggal');
var rekapId = $stateParams.rekapId;
console.log(rekapId);
childDate.child(rekapId).child('date').child('tanggalBaca').once('value', function(snap) {
var snapshot = snap.val();
$scope.tanggal = snapshot;
console.log($scope.tanggal);
})
// here outside function i want to access my $scope.tanggal;
}])
UPDATE What i want to achieve is i can use the result from firebase which inside $scope.tanggal to process in another function in same controller.
i tried many things including using $scope.$apply but nothing worked it's always return undefined when i am trying to console.log $scope.tanggal outside the snapshot function.
Hope this made you understand my question.
ANSWER
I found the answer here How to get variable value outside angular function because 'value' is async and create some delay so i came with the solution using comeback function
childDate.child(rekapId).child('date').child('tanggalBaca').once('value',function(snap){
var snapshot= snap.val();
$scope.$apply(function() {
$scope.tanggal = snapshot;
});
console.log($scope.tanggal);
myAfterFunction(); //here i call the function
})
function myAfterFunction(){
console.log($scope.tanggal); // The function is triggered after called in snapshot function this time $scope.tanggal has value from firebase
}