【问题标题】:md- datePicker - Date instance error alwaysmd- datePicker - 日期实例总是错误
【发布时间】:2017-01-22 07:44:48
【问题描述】:

我收到此错误The ng-model for md-datepicker must be a Date instance. Currently the model is a: string。我正在使用时刻..

在视图中

<md-datepicker ng-model="Model.currentContact.getSetIncorporated" ng-model-options="{ getterSetter: true }" md-placeholder="Enter date"></md-datepicker>

在模型中

Contact.prototype.getSetIncorporated = function(date) {
        if (arguments.length) {
            this.company.information.incorporatedObject = date;
            this.company.information.incorporated = moment.utc(date).format('X');
        }
        if (!this.company.information.incorporatedObject) {
            if (this.company.information.incorporated !== '') {
                this.company.information.incorporatedObject = moment.utc(this.company.information.incorporated, 'X').toDate();
            } else {
                this.company.information.incorporatedObject = null;
            }}
        return this.company.information.incorporatedObject;
    }

我也尝试了几个 mdLocale.formatDate 和 parseDate。当前版本是

$mdDateLocale.formatDate = function(date) {

            return moment(date).format('YYYY/MM/DD');
        };

        $mdDateLocale.parseDate = function(dateString) {

            var m = moment(dateString, 'YYYY/MM/DD', true);
            return m.isValid() ? m.toDate() : new Date(NaN);
        };

服务器正在发送这个字符串2016-09-10T22:00:00.000Z

当我使用 new Date() 将该字符串转换为 Date 对象时,我在 mdDatePicker 中得到了正确的结果,但我也得到了 Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! 这会破坏我的页面。

【问题讨论】:

    标签: javascript angularjs datepicker angular-material


    【解决方案1】:

    这很简单。您传递给Model.currentContact.getSetIncorporated 的值是字符串而不是日期。

    这是一个问题示例 - CodePen。控制台显示错误。

    angular.module('MyApp',['ngMaterial'])
    
    .controller('AppCtrl', function() {
      this.myDate = new Date();
      this.myDate = this.myDate.toString(); // Comment this out to work correctly
    });
    

    这个问题 - How to check whether an object is a date? - 将解释如何检查您传递的是字符串还是日期。

    更新:

    消息的原因

    未捕获的错误:[$rootScope:infdig] 已达到 10 次 $digest() 迭代。

    似乎是因为Contact.prototype.getSetIncorporated 正在返回一个日期。返回字符串有效,但 ng-model 需要日期!

    这是解决这个问题的一种方法 - CodePen

    angular.module('MyApp',['ngMaterial'])
    
    .controller('AppCtrl', function($scope) {
      this.myDate = new Date();
    
      this.test = function () {
        return new Date();
      }  
      this.testModel = this.test();
    });
    

    标记

    <div ng-controller="AppCtrl as vm" ng-cloak="" class="datepickerdemoBasicUsage" ng-app="MyApp">
      <md-content>
        <md-datepicker ng-model="vm.testModel"  ng-model-options="{ getterSetter: true }" ng-change="change()"></md-datepicker>
      </md-content>
    </div>
    

    【讨论】:

    • 我知道它是一个字符串...我什至尝试使用 new Date(stringDate) 转换该字符串,但出现无限摘要循环错误。
    • Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
    • @Alexa - 您可能需要添加您的问题,因为这可能是主要问题。
    • 那该怎么办?一定是个函数,就是给我的代码……可能转换成Date对象必须在mdDateLocale中完成,但是不行,我尝试了很多组合。
    • 我发现了一些东西,但不知道如何让它工作..它必须返回一个日期对象的相同实例.. 那么如何动态更改日期对象的值呢? stackoverflow.com/questions/33287911/…
    【解决方案2】:

    当你使用 moment 时,你必须从 moment 中获取 _d 属性:

        var _convertStringToDate = function (stringDate) {
    
            var date;
            if (angular.isString(stringDate)) {
                date = moment(stringDate)._d;
            }
    
            return date;
        };
    

    【讨论】:

    • 我收到了Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多