I am building a calendar. Problem is when I load the first time then dates are not showing. If I go to next month and the come back again to previous month then dates are showing and working fine. Suppose, present month is Oct, so when you load the page then Only current month Oct will be shown. No date. Now go to Nov by clicking on right arrow and come back again on Oct by clicking on left arrow. Date will be shown. Please check my fiddle and code :-
datepicker = angular.module('datepicker', []);
datepicker.controller('dateTimePicker', ['$scope', function($scope){
console.log('alive');
var date = new Date();
var months = [],
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
for (var i = 0; i <= 12; i++) {
months.push(monthNames[date.getMonth()] + ' ' + date.getFullYear());
date.setMonth(date.getMonth() + 1);
}
$scope.year =2015;
$scope.changeMonth = function(steps) {
if ($scope.monthIndex + steps >= 0 &&
$scope.monthIndex + steps <= 12
) {
$scope.dateValues = [];
$scope.monthIndex = $scope.monthIndex + steps;
$scope.monthName = $scope.months[$scope.monthIndex];
var date = new Date();
console.log(date.getMonth());
var offset = date.getMonth()
console.log($scope.monthIndex);
var offsetDate = offset + $scope.monthIndex;
$scope.nDays = new Date( $scope.year, offsetDate+1, 0).getDate();
console.log(offsetDate+1);
console.log(new Date( $scope.year, offsetDate, 1));
for (i = 1; i <= $scope.nDays; i++) {
var d = new Date();
$scope.dateValues.push(new Date($scope.year, offsetDate, i));
}
}else{console.log("missed")}
};
$scope.monthIndex = 0;
$scope.months = months;
$scope.monthName = months[0];
}]);
fiddle link :- https://jsfiddle.net/abhijitloco/fxbmpetu/15/