I've been working on an Angular controller to fetch records from a database and display them in a calendar. However, I'm facing an issue where the events array is returning empty. I tried using $rootScope.events as a workaround but encountered an error stating "concat is not a function of undefined." Could this be due to nested scopes causing some confusion?
Upon further investigation, I realized that the eachActivity variable is also undefined within the inner callback. It seems like there might be some fundamental knowledge missing on my end.
app.controller('Calendar', ['$scope','$rootScope','$resource','moment', function($scope, $rootScope, $resource ,moment) {
var Activities = $resource('/api/activities');
Activities.query(function(activities){
$rootScope.activities = activities;
//console.log($rootScope.activities);
});
//console.log($rootScope.activities);
var vm = this;
var events = [];
//setting up the calendar on rootScope so it can access Events data from other controllers
$rootScope.calendar = new moment();
angular.forEach($rootScope.activities, function(eachActivity){
//console.log(eachActivity.events);
if (eachActivity.events.length > 0){
angular.forEach(eachActivity.events, function(eachEvent, eachActivity){
console.log(eachEvent);
var entry = {
title: eachActivity.title,
type: "warning",
startsAt: eachEvent.startDate,
endsAt: eachEvent.endDate,
incrementBadgeTotal: true
}
events.concat(entry);
});
}
});
vm.events = events;
console.log(vm.events);
vm.calendarView = 'month';
vm.viewDate = moment().startOf('month').toDate();
vm.isCellOpen = true;
}]);