I have an angular controller that is supposed to get products from a database and then perform some operations on them. The controller has a function which has another function inside it. How can I return the data received from the nested function back to the controller? I've tried virtually everything I can think of and nothing seems to be working.
search.controller('SearchResultsController', function($scope, $routeParams, $http){
$scope.getAllProducts = function(){ //function 1
var config = {
method: "GET",
url: '/products',
headers: {"Content-Type": "application/json;charset=utf-8"}
};
$http(config).then(function(response) { //function 2
$scope.allProducts = response.data;
console.log($scope.allProducts) // correctly prints products fetched from server
});
$scope.getAllProducts();
console.log($scope.allProducts); //prints undefined
};
};