My controller is configured to load data from a server request initially, but I need to implement a refresh button that will send another request with updated parameters. The issue I'm facing is that the asynchronous nature of the request means the data is not available in time. How can I make my refresh method synchronous?
function DataController($scope, $http, $location,$filter) {
$scope.mydata=[];
$scope.requestUrl="";
$scope.requestConfig =[];
$scope.loadDatas = function () {
var params = {
offsetParam: $scope.offsetParam
};
var config = {
params: params
};
var url='server/loadmydata/';
$scope.requestUrl = url;
$scope.requestConfig = config;
//Fetch data from server
$http.get(url,config)
.success( function (data,config) {
$scope.mydata = data;
$scope.offsetParam =$scope.offsetParam + data.length;
})
.error(function(data,config){
$scope.error = data;
});
};
// Refresh fonction
$scope.refresh = function() {
var params = {
offsetParam: $scope.offsetParam,
};
$scope.requestConfig = {
params: params
};
$http({
method: "GET",
url : $scope.requestConfig,
params :params
}).then(function(data) {
if(data && data.length>0){
$scope.mydata = push(data);
$scope.offsetParam =$scope.offsetParam + data.length;
}
});
};
}
This snippet shows how the data is displayed in my view using AngularJS:
<div ng-repeat="data in mydata ">{{data.name}} </div>
I would appreciate any guidance on synchronizing the response of the refresh method. Thank you.