4

I am getting this error The ng-model for md-datepicker must be a Date instance. Currently the model is a: string. I am using moment..

in view

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

in model

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;
    }

Also I have tried several mdLocale.formatDate and parseDate. Current version is

$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);
        };

the server is sending this string 2016-09-10T22:00:00.000Z

When I convert that string to Date object with new Date(), I get right result showing in mdDatePicker but I get also Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! which brakes my page.

2 Answers 2

1

It's quite simple. The value you are passing to Model.currentContact.getSetIncorporated is a string rather than a date.

Here is an example of the problem - CodePen. The console shows the error.

angular.module('MyApp',['ngMaterial'])

.controller('AppCtrl', function() {
  this.myDate = new Date();
  this.myDate = this.myDate.toString(); // Comment this out to work correctly
});

This question - How to check whether an object is a date? - will explain how to check whether you are passing a string or a date.

Update:

The reason for the message

Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached.

seems to be because Contact.prototype.getSetIncorporated is returning a Date. Returning a string works but the ng-model requires a Date!

Here's a way to get round that problem - CodePen.

angular.module('MyApp',['ngMaterial'])

.controller('AppCtrl', function($scope) {
  this.myDate = new Date();

  this.test = function () {
    return new Date();
  }  
  this.testModel = this.test();
});

Markup

<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>
Sign up to request clarification or add additional context in comments.

7 Comments

I know it is a string... I even try converting that string with new Date(stringDate), but got infinite digest loop error.
Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
@Alexa - You may want to add that your question as that may be the main problem.
What to do then? It must be a function, that is the code given to me... Maybe converting to Date object must be done in mdDateLocale, but it is not working, I tried many combinations.
I found something, but don't know how to get it work..It must return same instance of an date object.. So how to dinamicaly change Date object valueOf? stackoverflow.com/questions/33287911/…
|
0

when you use moment you have to take the _d property from moment:

    var _convertStringToDate = function (stringDate) {

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

        return date;
    };

1 Comment

I got Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.