My AngularJS setup is causing issues with setting up a function in a controller.
Here is the code snippet from the controller:
$scope.totalUnpaid = 0;
$scope.totalPaid = 0;
$scope.totalHalfDue = 0;
$scope.totalHourDue = 0;
$scope.totalBuisness = 0;
$scope.totalUnpaidSesh = [];
$scope.totalPaidSesh = [];
const getTheSessions = () => {
adminService.getAllSessions().then((response) => {
console.log(response);
response.data.forEach(e => e.next_class = e.next_class.substring(0, 10));
$scope.allSessions = response.data;
for (var payment in $scope.allSessions) {
if ($scope.allSessions[payment].payment == 'Paid') {
$scope.totalPaid++;
$scope.totalPaidSesh.push($scope.trainerSesh[payment]);
} else {
$scope.totalUnpaid++;
$scope.totalUnpaidSesh.push($scope.allSessions[payment]);
}
}
for (var next_session in $scope.totalUnpaidSesh) {
if ($scope.totalUnpaidSesh[next_session].next_session == '30_Minute') {
$scope.totalHalfDue = $scope.totalHalfDue + 25;
} else if ($scope.unpaidSesh[next_session].next_session == 'Hour_Session') {
$scope.totalHourDue = $scope.totalHourDue + 50;
}
}
$scope.totalBuisness = $scope.totalHalfDue + $scope.totalHourDue;
});
};
I am facing an issue where the requested data doesn't show up in the controller even though it's present when I log it in the service.
Strangely enough, when I remove the `$scope` and directly access the service like this:
$scope.totalUnpaid = 0;
$scope.totalPaid = 0;
$scope.totalHalfDue = 0;
$scope.totalHourDue = 0;
$scope.totalBuisness = 0;
$scope.totalUnpaidSesh = [];
$scope.totalPaidSesh = [];
adminService.getAllSessions().then((response) => {
console.log(response);
response.data.forEach(e => e.next_class = e.next_class.substring(0, 10));
$scope.allSessions = response.data;
for (var payment in $scope.allSessions) {
if ($scope.allSessions[payment].payment == 'Paid') {
$scope.totalPaid++;
$scope.totalPaidSesh.push($scope.trainerSesh[payment]);
} else {
$scope.totalUnpaid++;
$scope.totalUnpaidSesh.push($scope.allSessions[payment]);
}
}
for (var next_session in $scope.totalUnpaidSesh) {
if ($scope.totalUnpaidSesh[next_session].next_session == '30_Minute') {
$scope.totalHalfDue = $scope.totalHalfDue + 25;
} else if ($scope.unpaidSesh[next_session].next_session == 'Hour_Session') {
$scope.totalHourDue = $scope.totalHourDue + 50;
}
}
$scope.totalBuisness = $scope.totalHalfDue + $scope.totalHourDue;
});
The data shows up as expected.
Why does it work without the method `getTheSessions`, but not when it is included?