Krishna shidnekoppa
Krishna shidnekoppa

Reputation: 1059

How to assign data returned from function to $scope variable

Here is my code,

$scope.output=abc(1,2);
    function abc(mid, type) {
       $http({
               ...
             }
           }).then(function (response) {
                return response.data;
          }, function (response) {
         });
      }
    console.log($scope.output)

$scope.output

is

undefined

Function is executing but data is not assigning to $scope variable

Upvotes: 1

Views: 747

Answers (2)

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27222

Instead of using return in async call. You can directly assign the response.data into the $scope.output variable.

abc(1,2);
function abc(mid, type) {
   $http({
           ...
         }
       }).then(function (response) {
            $scope.output = response.data;
      }, function(error) {
     });
  }

Upvotes: 0

Ankit Kumar Gupta
Ankit Kumar Gupta

Reputation: 1322

abc(1,2);
function abc(mid, type) {
   $http({
           ...
         }
       }).then(function (response) {
            $scope.output = response.data;

      }, function (response) {
     });
  }
console.log($scope.output)

In async operations, you cannot use return

Upvotes: 1

Related Questions