Maria Jane Maria Jane - 1 month ago 16
Javascript Question

ng-init in angularjs didn't work for me

I try to set the dropdown to current month which is Oct but it doesn't work, any clue guys?

http://plnkr.co/edit/qtPvCy4phNQW6I6IPQyr?p=preview

app.controller('MainCtrl', function($scope) {
var d = new Date();
var month = new Array();

var this_year = d.getFullYear();

month[0] = "January " + this_year;
month[1] = "February " + this_year;
month[2] = "March " + this_year;
month[3] = "April " + this_year;
month[4] = "May " + this_year;
month[5] = "June " + this_year;
month[6] = "July " + this_year;
month[7] = "August " + this_year;
month[8] = "September " + this_year;
month[9] = "October " + this_year;
month[10] = "November " + this_year;
month[11] = "December " + this_year;

$scope.months = month;

$scope.this_month = month[d.getMonth()] + " " + this_year; // won't work?
});

Answer

You can set default month by assigning current month to model selected_month.

$scope.selected_month = $scope.months[d.getMonth()];

Plunker

Comments