User1628746328 posted
Hey There.
New to Web API and am stuck for about a week now.
I am following
this article
The author is using Angular for his client controller and is fetching records from a database, and I have implemented something similar:
//define angularJS module
var app = angular.module('FFPA', []);
app.controller('myCtrl', function ($scope, $http, EmployeesService) {
var data;
$scope.employeesData = null;
//Fetches records from the factory at bottom of script file
EmployeesService.GetAllRecords().then(function (d) {
$scope.employeesData = d.data; //success.
data = d.data;
alert("data"+data);
}, function () {
alert('Error Occurred!') //failed.
});
And then there's a factory implemented outside the Angular controller:
app.factory('EmployeesService', function ($http) {
var fac = {};
fac.GetAllRecords = function () {
return $http.get('api/Employee/GetAllEmployeesByFloor?Floor=3');
}
return fac;
});
The problem is that I need to wait until data is returned to do any work, so how can I implement a promise to wait until data is returned from my service?
For example:
//need to wait until service returns data to do this or I get an error when data.filter executes..
groups.each(function(i,d){
var thisData = data.filter(function (d) {
Do some stuff..
}
});
});
Thanks.