简体   繁体   English

单击时,Angular Material md-datepicker 会引发错误

[英]Angular Material md-datepicker throws error when clicked

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我是新来的,最近使用 Angular Material 0.11.0 的 md-datepicker 遇到了一些问题: 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.添加和更新都是通过 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.工厂函数包含我配置的 $mdDialog 服务。 I was able to update and add without any problems until I added the md-datepicker.在添加 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.环顾四周后,我发现我应该配置 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).我从https://material.angularjs.org/HEAD/#/api/material.components.datepicker/service/ $mdDateLocaleProvider 中获取灵感,并将以下代码作为 .config 添加到我的主 AngularJS 应用程序中(我使用 momentjs 来提供用户日期基于他们的语言环境)。

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.我什至可以添加我选择的日期,并且可以在纪元时间将其保存到 firebase。

        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).当我专门单击 md-datepicker 的日历图标来更改日期时(一旦更新 $mdDialog 打开),就会出现问题。

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.此时 md-datepicker 不知道它是哪一年,从 1933 年左右开始。奇怪的是,如果我不点击图标并首先更改输入中的日期,我可以点击带出日历的图标在正确的日期。 I can do this as long as my current mdDialog is not closed.只要我当前的 mdDialog 没有关闭,我就可以做到这一点。 Then, when I click update, it sometimes updates the date on firebase.然后,当我单击更新时,它有时会更新 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.如果我保持 md-datepicker 不变,我可以很好地更改我的其他数据。

        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.谢谢大家的时间。


I had the same issue as you did before you added the configuration of $mdDateLocaleProvider.在添加 $mdDateLocaleProvider 的配置之前,我遇到了与您相同的问题。 I kept getting TypeError: date.toLocaleDateString is not a function.我不断收到 TypeError: date.toLocaleDateString is not a function。

I was also using firebase and was able to push without any problems.我也在使用 firebase 并且能够毫无问题地推送。 I could even view the datepicker with the saved result, it was only on ref.update that I would get the error.我什至可以用保存的结果查看日期选择器,只有在 ref.update 上我才会收到错误。 I spent quite a bit of time hunting down the bug and finally discovered that angular material was running my date through it's date function twice due to angular's digest cycle and the second time through it was using the string representation of the js Date object and not the Date obj itself.我花了很多时间来寻找错误,最后发现由于 angular 的摘要循环,angular 材料通过它的 date 函数两次运行我的日期,第二次通过它使用的是 js Date 对象的字符串表示,而不是日期 obj 本身。 (hence the TypeError: date.toLocaleDateString is not a function - because it was treating date as the String and not a date Obj.) (因此 TypeError: date.toLocaleDateString 不是函数 - 因为它将日期视为字符串而不是日期对象。)

Unfortunately, I could not come up with a fix in my actual app so I had to go into the angular-material.js source and right before the error hit I took the date that was being passed in and ensured it was being converted to a date (d = new Date(d));不幸的是,我无法在我的实际应用程序中找到修复程序,所以我不得不进入 angular-material.js 源代码,就在错误发生之前,我获取了传入的日期并确保它被转换为日期(d = 新日期(d));

This worked for me as it was always either a date or the toString of the date - either way it ensures that it's a actual date object.这对我有用,因为它始终是日期或日期的 toString - 无论哪种方式,它都确保它是一个实际的日期对象。

The following code was left as it is in the question.以下代码在问题中保持原样。

app.config(function($mdDateLocaleProvider) {
$mdDateLocaleProvider.formatDate = function(date) {
   return moment(date).format('L');
};});

This eradicates the 'TypeError: date.toLocaleDateString is not a function' error.这消除了“TypeError: date.toLocaleDateString is not a function”错误。

Throughout I have only been interested in the epoch.在整个过程中,我只对时代感兴趣。 I didn't realise that the md-datepicker only likes date objects.我没有意识到 md-datepicker 只喜欢日期对象。 This explains why I could not update project.deadline as it was reading epoch time.这解释了为什么我无法更新 project.deadline 因为它正在阅读纪元时间。 It was able to see it fine, but changing it resulting in errors as it could not identify which date to show (hence showing 1934..).它能够很好地看到它,但是更改它会导致错误,因为它无法确定要显示的日期(因此显示 1934 ..)。

Instead I focused on dealing with datepicker only with date objects.相反,我只专注于处理日期选择器和日期对象。

Adding new project:添加新项目:

  • var date is the input from md-datepicker in date Object form. var date 是来自 md-datepicker 的 date Object 形式的输入。 I then convert it into epoch so save it to firebase just like the other values.然后我将其转换为纪元,以便像其他值一样将其保存到 firebase。

     var date = new Date(project.deadline); project.deadline = date.getTime();

Updating project:更新项目:

  • Assigning currentProject to vm.project.将 currentProject 分配给 vm.project。 This gives me access to vm.project.deadline which includes the deadline I added in epoch time.这使我可以访问 vm.project.deadline,其中包括我在纪元时间添加的截止日期。 Convert deadline from epoch time to date object.将截止日期从纪元时间转换为日期对象。 This allows me to change the date as I like.这使我可以根据需要更改日期。 Once the user clicks update, the new Date Object is first converted into epoch and then I can add to firebase just like other data.一旦用户点击更新,新的日期对象首先被转换为纪元,然后我可以像其他数据一样添加到 firebase。

     var vm = this; vm.project = {}; vm.project = currentProject; vm.project.deadline = new Date(vm.project.deadline); vm.update = function() { var date = new Date(vm.project.deadline); vm.project.deadline = date.getTime(); ...

I needed the epoch times hence the conversions.我需要时代,因此需要转换。 If one has no need for epoch (or other date forms) I imagine leaving the date object unchanged should have no problem.如果不需要纪元(或其他日期形式),我想保持日期对象不变应该没有问题。

I had a similar error and the cause turned out to be that I was defining $mdDateLocale.formatDate for output (Date->string) but I had not defined $mdDateLocale.parseDate for input (string->Date).我有一个类似的错误,原因竟然是我被定义$mdDateLocale.formatDate输出(日期- >字符串),但我却没有定义$mdDateLocale.parseDate输入(与字符串>日期)。 Once I did that, the error went away.一旦我这样做了,错误就消失了。

Note that both functions have to deal with input that could be undefined/string/Date during Angular's digest cycle.请注意,这两个函数都必须处理在 Angular 的摘要周期中可能是 undefined/string/Date 的输入。 So be sure to test the input before you .split or .parse or whatever.所以一定要在你 .split 或 .parse 或其他任何东西之前测试输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular材质md-datepicker中的空值 - Null value in Angular material md-datepicker 在Angular材质中以编程方式打开md-datepicker - Open md-datepicker in Angular Material Programmatically 角材料md-datepicker和md-select验证 - Angular Material md-datepicker and md-select validation 如何在角度材料设计中为md-datepicker添加“浮动标签”? - How to add “floating label” for md-datepicker in angular material design? 使用momentjs更改Angular Material中md-datepicker的格式 - Change format of md-datepicker in Angular Material with momentjs 角材料: <md-datepicker> 内 <md-tabs> 渲染不正确 - Angular Material: <md-datepicker> inside <md-tabs> does not render properly 角材料(md-datepicker)-如何在一页中将不同的日期格式设置为不同的日期选择器 - angular material (md-datepicker) - how to set different date format to different datepicker in one page Angular Material,Md-datepicker - 在输入点击时打开日期选择器 - Angular Material, Md-datepicker - open date-picker on input click 如何将Angular Material md-datepicker的日期值初始化为任何指定日期 - How to initialize the date value of Angular Material md-datepicker to any specified date 如何动态设置Angular Material md-datepicker指令的md-min-date / md-max-date? - How to dynamically set md-min-date/md-max-date of Angular Material md-datepicker directive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM