Get the value of a variable outside of Promise

I have a service in AngularJs that will return a value from a database.

userData.getUserData(function(response) {
  $scope.uid = response
});
Run codeHide result

when I inject this service into my controller, it will return a promise, But I need this Promise value Outside of my function, how can I do it?

Link to PLunkr

+4
source share
3 answers

From your plunker code, you have a service that looks like this:

angular.module('plunker');
.service('myService', function($firebaseRef, $firebaseObject){

    this.getUserData = function(el) {
            $firebaseObject($firebaseRef.users.child(this.localStorage().uid)).$loaded(function(data) {
                el(data);
            })
        }
});

and such a controller:

app.controller('MainCtrl', function($scope, myService) {

 myService.getUserData(function(response) {  
   $scope.uid = response;
 })

 console.log($scope.uid);  

 $scope.postRequest = function(val) {
            $firebaseArray($firebaseRef.requests.child($scope.uid)).$add(val);
            console.log(val)
            console.log($scope.request);
        }
});

The problem is that the line console.log($scope.uid);is printing undefined.

, getUserData , , response, (Firebase) , console.log.

function(response) { $scope.uid = response; } , (HTTP 2xx), . , , , , + , . , 150 .

, , console.log, response , .. $scope.uid , , console.log undefined.

, , , . , - :

app.controller('MainCtrl', function($scope, myService) {

  myService.getUserData(function(response) {  
    $scope.uid = response;
    console.log($scope.uid);
    // and any other code which depends on the $scope.uid
  });

  // ...

});

AngularJS promises $q. , :

angular.module('plunker');
.service('myService', function($q, $firebaseRef, $firebaseObject){
    var deferred = $q.defer();

    this.getUserData = function(el) {
            $firebaseObject($firebaseRef.users.child(this.localStorage().uid)).$loaded(function(data) {
              deferred.resolve(data);
            });
    };

    return deferred.promise;
});

:

app.controller('MainCtrl', function($scope, myService) {

 myService.getUserData()
   .then(function(data) {
     $scope.uid = data;
     console.log($scope.uid);
     // and any other code 
     // you can also return promises here and then chain
     // them, read about AngularJS promises
   });

   // ...
});

, , , .

, postRequest, $scope.uid. , , $scope.uid. , - , , . , - $scope.uid. , :

<button type="button" ng-click="postRequest(something)" ng-disabled="uid === undefined">Post</button>

, .

+3

, $scope.uid , -.

, , , var . , response , - :

$scope.uid = {};

userData.getUserData(function(response) {
  $scope.uid = response;
});

var undefined. , , , , .

,

userData.getUserData(function(response) {
  $scope.uid = response;
  console.log($scope.uid);
});

, , , ;

userData.getUserData(function(response) {
  $scope.uid = response;
});

console.log($scope.uid);

, , . , , .

TL: DR $scope.uid , , , , , , undefined

UPDATE:

 userData.getUserData(function(response) {
  $scope.postRequest(response);
 });
 $scope.postRequest = function(val) {
    $firebaseArray($firebaseRef.requests.child($scope.uid)).$add(val);
    console.log(val) console.log($scope.request);
 }

: https://plnkr.co/edit/KbVoni3jfnHm54M80kYl?p=preview

+1

, userData.getUserData.

, , :

function getUserData(callback){
  userData.getUserData(function(response) {
    callback(response);
  });
}

getUserData(function(response){
  $scope.uid = response;
  // then you can proceed and use the $scope.uid here
});

getUserData(function(response){
  callAnotherFunction(response);
});

function callAnotherFunction(response){
  console.log(response);
  // You can use the value inside this function
}

$timeout, $scope.uid

-1

All Articles