I'm trying return the data from the function getShiftDetails()
SS_Mod_App.controller("SS_Ctrl", function ($scope, $http, $location, $window, $sce) {
var ShiftDetails = []; var ShiftDtls = [];
ShiftDtls = getShiftDetails();
// alert(ShiftDtls); --> ShiftDtls undefined
insertDaysHtml(ShiftDtls);
function getShiftDetails() {
$http({
method: 'GET',
url: 'http://xxxxx/api/Shift/GetShiftDetails',
params: { }
, headers: { 'Content-Type': 'application/json; charset=utf-8', 'dataType': 'json' }
}).then(function successCallback(response) {
ShiftDetails = response.data; --> successful response with data
alert(JSON.stringify(ShiftDetails.length)); --> getting length 21 here
return ShiftDetails;
}, function errorCallback(response) { });
}
and want to pass it to the next function insertDaysHtml where I want iterate it and fetch data of the ShiftDetails as below but getting errors as below .
function insertDaysHtml(ShiftDtls) {
alert(JSON.stringify($scope.ShiftDetails.length)); --> Length "0" here
//alert(JSON.stringify(ShiftDtls.length)); Error msg: TypeError: Cannot read property 'length' of undefined
// if (ShiftDtls.length > 0) { --> Error msg: TypeError: Cannot read property 'length' of undefined
if ($scope.ShiftDtls.length > 0) {
// alert(ShiftDtls.length );
dayhtmlContent = dayhtmlContent + '<tr> '
for (var j = 0; j <= ShiftDtls.length; j++) {
dayhtmlContent = dayhtmlContent + '<td> $scope.ShiftDtls[ ' + j + '].ShiftName[0]));'
}
$scope.divHtmlDay = $sce.trustAsHtml(dayhtmlContent);
}