I'm new here and have been having some problems with the recently available md-datepicker by Angular Material 0.11.0: https://material.angularjs.org/0.11.0/#/demo/material.components.datepicker
In short, my app allows users to add individual projects with name and description. They are also able to update relevant project details.
Both adding and updating are done in through Material Designs mdDialog. Once a user clicks add or update, the controller calls the relevant function which calls a corresponding function in my factory.
The factory functions contain the $mdDialog service which I have configured. I was able to update and add without any problems until I added the md-datepicker.
I got the following error when I clicked to edit.
TypeError: date.toLocaleDateString is not a function
at Object.defaultFormatDate [as formatDate] (angular-material.js:6858)
at DatePickerCtrl.configureNgModel.ngModelCtrl.$render (angular-material.js:7182)
at Object.ngModelWatch (angular.js:25353)
at Scope.parent.$get.Scope.$digest (angular.js:15751)
at Scope.parent.$get.Scope.$apply (angular.js:16030)
at done (angular.js:10545)
at completeRequest (angular.js:10717)
at XMLHttpRequest.requestLoaded (angular.js:10658)
After looking around, I figured out that I should configure the md-datepicker. I took inspiration form https://material.angularjs.org/HEAD/#/api/material.components.datepicker/service/$mdDateLocaleProvider and added the following code as .config to my main AngularJS App (I am using momentjs to give users date based on their locale).
app.config(function($mdDateLocaleProvider) {
$mdDateLocaleProvider.formatDate = function(date) {
return moment(date).format('L');
};});
Now I can add a project with the following code in my 'add' factory. I can even add a date of my choice and I can save it to firebase in epoch time.
return $mdDialog.show({
clickOutsideToClose: true,
controller: function($mdDialog) {
var vm = this;
vm.project = project;
vm.add = function(project) {
var date = new Date();
project.deadline = date.getTime();
if(project.deadline == undefined) {
project.deadline = null;
};
FBprojects.$add(project);
console.log(project);
$mdDialog.hide();
};
},
controllerAs: 'PAmodal',
templateUrl: 'views/newproject.html',
targetEvent: e
})
The problem arises when I specifically click on the calendar icon of md-datepicker to change the date (once update $mdDialog is open).
TypeError: date.getFullYear is not a function
at CalendarCtrl.getDateId (angular-material.js:6427)
at angular-material.js:6349
at processQueue (angular.js:14678)
at angular.js:14694
at Scope.parent.$get.Scope.$eval (angular.js:15922)
at Scope.parent.$get.Scope.$digest (angular.js:15733)
at Scope.parent.$get.Scope.$apply (angular.js:16030)
at HTMLButtonElement.<anonymous> (angular.js:23486)
at HTMLButtonElement.jQuery.event.dispatch (jquery.js:4435)
at HTMLButtonElement.jQuery.event.add.elemData.handle (jquery.js:4121)(anonymous function) @ angular.js:12450ident.$get @ angular.js:9237processQueue @ angular.js:14686(anonymous function) @ angular.js:14694parent.$get.Scope.$eval @ angular.js:15922parent.$get.Scope.$digest @ angular.js:15733parent.$get.Scope.$apply @ angular.js:16030(anonymous function) @ angular.js:23486jQuery.event.dispatch @ jquery.js:4435jQuery.event.add.elemData.handle @ jquery.js:4121
Uncaught TypeError: end.getFullYear is not a function
TypeError: date.getFullYear is not a function
at CalendarCtrl.getDateId (http://localhost:9000/bower_components/angular-material/angular-material.js:6427:12)
at CalendarCtrl.focus (http://localhost:9000/bower_components/angular-material/angular-material.js:6298:23)
at http://localhost:9000/bower_components/angular-material/angular-material.js:7372:30
at http://localhost:9000/bower_components/angular-material/angular-material.js:1068:11
at Array.forEach (native)
at processQueue (http://localhost:9000/bower_components/angular-material/angular-material.js:1067:15)
at http://localhost:9000/bower_components/angular/angular.js:17788:31
at completeOutstandingRequest (http://localhost:9000/bower_components/angular/angular.js:5498:10)
at http://localhost:9000/bower_components/angular/angular.js:5775:7
At this point the md-datepicker doesnt know which year it is and start at around the year 1933. Strangely if I don't click on the icon and change the date in the input first, I can click the icon which brings out the calendar on a correct date. I can do this as long as my current mdDialog is not closed. Then, when I click update, it sometimes updates the date on firebase. I still have no figured out why it is not consistent. The following code is called via controller for updating project
. If I leave the md-datepicker untouched, I can change my other data fine.
updateProject: function(e, currentProject) {
return $mdDialog.show({
clickOutsideToClose: true,
controller: function($mdDialog) {
var vm = this;
vm.project = {};
vm.project = currentProject;
var pid = $stateParams.pid;
var n = vm.project.deadline;
var d = new Date(n).toString();
vm.update = function() {
//Updates at FB locations with updated project
ref.child('projects').child(pid).update({
title: vm.project.title,
description: vm.project.description,
deadline: d
});
$mdDialog.hide();
};
},
controllerAs: 'PEmodal',
templateUrl: 'views/updateproject.html',
parent: angular.element(document.body),
targetEvent: e
});
I have been stuck on this problem for sometime with no solution, and would appreciate some light on this. Thank you all for your time.
clickOutsideToClose: true
. May be dependent on your scoping. Try changing it tofalse
to see if you can get it functioning! – Ryan DrakeDate
object (it was amoment.format()
). Usingnew Date()
fixes the thing. See material.angularjs.org/latest/api/directive/mdDatepicker – Quentin Klein