0

I am using angular js for my project. Currently I am getting data using below function. This is my function.

  $scope.loadMoreDetails = function () {
    $http({
      method: "GET",
      url: appConfig.apiUrl + "/results/" + $scope.resultId + "/details/"
    }).then(function (response) {
      $scope.sourceData = response.data.sources;
      $scope.sourceList = $scope.sourceData.split(', '); // split string on comma space
      console.log($scope.sourceList);
    }, function (response) {
      $log.error(response);
    });
  };

My console log inside the function is working well, but I am trying to access that $scope.sourceList in another function.

$scope.saveAp = function () {
  console.log($scope.sourceList)
}

This console.log is undefined. Actually I need to know, how I access $scope.sourceList outside the function. How I do it.

Jack A.
  • 4,245
  • 1
  • 20
  • 34
Kumara
  • 471
  • 2
  • 10
  • 33
  • 1
    Does this answer your question? [How to return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – ASDFGerte Jun 02 '21 at 11:24
  • Are the `loadMoreDetails` and `saveAp` functions both attached to the same `$scope` instance? Are you waiting for the asynchronous operation to complete before calling `saveAp`? – Jack A. Jun 02 '21 at 16:30
  • yes same controller. two different functions – Kumara Jun 02 '21 at 16:42
  • If the same `$scope` instance is in use then the value assigned to `$scope.sourceList` will be available in `saveAp` as long as the assignment has been done before `saveAp` is called. So the question remains: are you waiting for the asynchronous operation in `loadMoreDetails` to complete before triggering `saveAp`? You should include the code that shows how these functions are triggered. – Jack A. Jun 03 '21 at 12:35

0 Answers0