0

I'm making http get call from angularJS function. Call works perfectly and get the response back but I need to store response in a variable so I can use it outside of $http. I tried to keep in $scope.data.submissionFileID but alert($scope.data.submissionFileID) says undefined. Also I want me make this call synchronous. I'm new to this, can you please help to modify this below code?

     $scope.openWindow = function (e) {
        
        var url = '/Submission/GetSubmissionFileInfo?' + 'id=' + SubmissionID;
        $http.get(url).success(function (response) {
        $scope.data.submissionFileID = response; // response is an integer such as 123
        }); 
        alert($scope.data.submissionFileID); // This is undefined, what should I do to fix it?
        
        var content = "<h7><b>" + "Created at  </b>" + $scope.data.submissionFileID + "</h7><br><br>";
        
    }
Partha
  • 413
  • 2
  • 5
  • 16
  • 1
    https://stackoverflow.com/q/14220321/3001761 – jonrsharpe Apr 24 '21 at 22:18
  • I want to make this call Synchronous because other part of the function I want my code to wait. – Partha Apr 24 '21 at 22:33
  • Well you can't, because a network request is fundamentally asynchronous. – jonrsharpe Apr 24 '21 at 22:36
  • How can I make my code wait then because it's preparing a content and it is not able to wait till response comes back. added the content I'm preparing with response. var content = "" + "Created at " + $scope.data.submissionFileID + "

    ";
    – Partha Apr 24 '21 at 22:58

1 Answers1

0

Something to consider is that the alert(...) is being called before the async function has a chance to complete. An option would be to send the response off to another function that sets the $scope variable and does whatever else you might want.

$scope.openWindow = function (e) {
var url = '/Submission/GetSubmissionFileInfo?' + 'id=' + SubmissionID;
   $http.get(url).success(function (response) {
      $scope.doTheThing(response);
   }); 
}

$scope.data = {}

$scope.doTheThing = function(response) {
   $scope.data.submissionFileID = response;
}

in the HTML template file...

<div ng-if="!data.submissionFileID">Retrieving Data....</div>
<div ng-if="data.submissionFileID">
<h7><b>Created at </b> {{data.submissionFileID}}</h7>
</div>
Kinglish
  • 23,358
  • 3
  • 22
  • 43
  • Thank you John. getting an exception in doTheThing when it's trying to set the response - TypeError: Cannot set property 'submissionFileID' of undefined. – Partha Apr 24 '21 at 22:54
  • I guess $scope.data doesn't exist yet? I have edited my answer – Kinglish Apr 24 '21 at 23:00
  • Yes! exception is fixed. But here things get bit tricky. If we put an alert($scope.data.submissionFileID) right after the call in openWindow function it still says as undefined because of asynchronous call. Code doesn't wait until it gets set in doTheThing function. Untimately I've to use $scope.data.submissionFileID when I'm preparing that content. I've added that line of code last line. Any idea what we can do to fix it? – Partha Apr 24 '21 at 23:08
  • Just have a placeholder that switches out with ng-if. I edited my answer – Kinglish Apr 24 '21 at 23:28