Hi,
I am trying to call httpget from service to webAPI controller to get data,
which is working fine.
I making a for loop in angular controller and calling the service each time
to get data and return back.
I unable to retrieve the response before the loop completes execution, it
sends the data only after the loop execution is completed.
I have herewith posted my controller and service code,I have even tried
using promises which is not working
AngularController.js
mainApp.controller('MainCtrl',
['$scope', 'AngularService', '$q','MainService',
function ($scope,AngularService,$q,MainService) {
$scope.tempArray = ["Draft", "Draft", "RequestApproval", "Issue",
"Approve", "RequestApproval", "Approve", "Draft", "Issue", "Approve",
"Draft", "RequestApproval", "Issue"];
$scope.name = "Sridhar Babu";
$scope.statusArray = ["", "Draft", "RequestApproval", "Approve",
"Issue"];
$scope.Map = [];
$scope.UpdatedResult = "Fail";
$scope.ResultArray = ["Str1","Str2","Str3"];
var chain = $q.when();
$scope.booleanValue = false;
$scope.ValidationCheck = function () {
angular.forEach($scope.ResultArray, function (item) {
var res= getConfirmationPopupForConfirmAndComment(item,
"action");
console.log(res);
}
};
var getConfirmationPopupForConfirmAndComment = function (message,
action) {
AngularService.ConfirmAndAct(item, action).then(function
(result) {
return result.data;
}
}, function (data) {
console.log("Error - AngularService-ConfirmAndAct");
});
};
}]);
AngularService.js
mainApp.service('AngularService', ['$http', '$q', function ($http, $q) {
this.GetHTTPFunction = function(message)
{
var url = '/api/List?str=' + message;
return $http.get(url);
}
this.ConfirmAndAct = function (message, action) {
var deferred = $q.defer();
this.GetHTTPFunction(message).then(successCallback, errorCallback);
function successCallback(response) {
console.log("Success");
console.log(response);
result = response;
deferred.resolve(response);
};
function errorCallback(error) {
console.log("Error in Getting Data");
};
return deferred.promise;
};
}]);
*OUTPUT:*
*undefined*
*undefined*
*undefined*
Note:
I need to get the value of " Str1,Str2,Str3"
$http.get() is being made, but after the ValidationCheck() function
completes execution
Expected output in the console
Str1
Str2
Str3
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.