2016-09-14 3 views
4

このエラーが発生しましたThe ng-model for md-datepicker must be a Date instance. Currently the model is a: string。私はまた、私はいくつかのmdLocale.formatDateとはparsedataを試してみましたモデルに...ビューでmd- datePicker - 日付インスタンスエラー常に

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

を瞬間を使用しています。現在のバージョンでは、私は、新しいDate()とDateオブジェクトにその文字列を変換する場合

$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

を送信しているですが、私はmdDatePickerに示す右側の結果を得るが、私はまた Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! を取得しています私のページを制圧する。

答えて

1

非常に簡単です。​​に渡す値は、日付ではなく文字列です。

問題の例を次に示します。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? - あなたは、文字列や日付を渡しているかどうかを確認する方法を説明します。

更新:

メッセージ

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

理由はContact.prototype.getSetIncorporatedは日付を返しているためと考えられます。文字列を返すと、ng-modelにDate!が必要です。

この問題を回避する方法は - CodePenです。

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

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

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

あなたが瞬間から_d財産を取らなければならない瞬間を使用するときにマークアップ

<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> 
+0

を、私はそれが文字列である知っている...私もその文字列を変換してみてください新しい日付(stringDate)で、無限のダイジェストループエラーが発生しました。 – Alexa

+0

'未知のエラー:[$ rootScope:infdig] 10 $ digest()反復に達しました。中止! ' – Alexa

+0

@Alexa - あなたの質問を主な問題として追加することができます。 –

0

var _convertStringToDate = function (stringDate) { 

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

     return date; 
    }; 
+0

私は を取得しました。 'Uncaught Error:[$ rootScope:infdig] 10 $ digest()回の繰り返しに達しました。中止! ' – Alexa

+0

あなたのコードを掲載してください。 – milan

関連する問題