-2

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.

enter image description here

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
    }
Community
  • 1
  • 1
Samuel Billy
  • 77
  • 4
  • 14
  • If it's not straight available with `$scope.tanggal` you should try using `$scope.apply()`function inside your function where you declare the scope variable. – thepio Sep 08 '16 at 10:28
  • your question isn't very clear; it seems to be asking how to access a variable that should be accessible in that location, indicating that either you are receiving an error when you try it that you didn't provide, or you didn't try the solution at all. – Claies Sep 08 '16 at 11:06
  • Hi @SamuelBilly Is your post a duplicate to the one where you found the answer? If so, you can actually flag your own post as duplicate. :) – AL. Sep 08 '16 at 11:40
  • If it's not a duplicate, can you provide your solution as an answer? That is totally acceptable on StackOverflow and is clearer to future visitors than combining the answer with your original question. – Frank van Puffelen Sep 08 '16 at 13:53

2 Answers2

1

You can directly access it, just try console.log($scope.tanggal);, If your function is not in angularjs scope, you can use $scope.$apply

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);
        })

console.log($scope.tanggal);
Ruhul
  • 990
  • 7
  • 15
0

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 
}
Community
  • 1
  • 1
Samuel Billy
  • 77
  • 4
  • 14