Noor
Noor

Reputation: 20168

Update variable in promise in Angular 1

I have an issue with a variable which is not being updated in the callback function of a promise as in the block of code below:

 $scope.showSelected= function (node){
                var promise = $http.get("http://127.0.0.1:5000/getResource?ldpr="+node.iri);
                promise.then(function(result){
                    node = result.data;
                });
        };

$scope.showSelected is a callback used by a widget. It has a parameter node which I am trying to update within the callback of the promise. How can I update this variable within callback of the promise

Upvotes: 0

Views: 515

Answers (1)

guest271314
guest271314

Reputation: 1

No value is returned from $scope.showSelected function. return a value from the asynchronous function call, use .then() to perform a task when the asynchronous call which returns a Promise completes

 $scope.showSelected = function (node){
                         return $http.get("http://127.0.0.1:5000/getResource?ldpr="+node.iri);
                       };

 $scope.showSelected(node)
 .then(function(result) {
   // do stuff with `result` : `node`
 })
 .catch(function(err) { // handle error
   console.log(err)
 })

Upvotes: 1

Related Questions