blaa blaa - 5 months ago 36
AngularJS Question

set current date on datepicker AngularJS

So, a modal opens up when I click on a button on m md-card to select a date. This is the code for it:

<!-- Modal -->
<md-dialog aria-label="Date Picker">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">{{modalTitle}}</h4>
</div>
<!-- /.modal-header -->
<div class="modal-body">
<md-input-container flex> <input type="date"
ng-model="startDateL"> </md-input-container>
<md-input-container flex> <label>{{}}</label> <input
type="date" ng-model="endDateL"> </md-input-container>
<!-- /.modal-search-box -->

<md-button ng-click="setDate()" class="md-primary">Update
Date</md-button>
</div>
<!-- /.modal-body -->
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
</md-dialog>
<!-- /.modal -->


and this is the code for my controller to select a date

function datePickerController($scope, $mdDialog, $rootScope, datePickerFactory, $filter){
console.log("suntem in date picker sel");
$scope.startDateL = new Date($rootScope.startDate);
$scope.endDateL = new Date($rootScope.endDate);

$scope.close = function() {
$mdDialog.hide();
console.log("closing");
}



$scope.setDate = function(startDate, endDate) {
$rootScope.startDate = $scope.startDateL;
$rootScope.endDate = $scope.endDateL;

var stDate = $filter('date')($rootScope.startDate,'yyyy-MM-dd');
var enDate = $filter('date')($rootScope.endDate,'yyyy-MM-dd');

console.log(stDate);
console.log(enDate);

datePickerFactory.save({sDate: stDate, eDate: enDate}, function(){
})

localStorage.setItem('chosenStartDate', JSON.stringify($rootScope.startDate));
localStorage.setItem('chosenEndDate', JSON.stringify($rootScope.endDate));
$rootScope.$broadcast("dateWasChosen");
$mdDialog.hide();

}

}


Now, what i want to do is to set a current date on my date field in my modal so that I have a default date when i open my modal.Is there a function that can set the current date?

Answer

You have to create a date object in your controller.

$scope.startDateL = new Date();
$scope.endDateL  = new Date();
Comments