角材料日期选择器字段值空

Kam*_*war 3 datepicker angularjs material-design

我想获得用户的出生日期,预定义的最小和最大日期工作正常.我想要的日期格式是DD-MM-YYYY,为此我在config中定义了以下内容;

app.config(['$mdDateLocaleProvider', function ($mdDateLocaleProvider) {
      $mdDateLocaleProvider.formatDate = function(date) {
         return moment(date).format('DD-MM-YYYY');
      }}]);
Run Code Online (Sandbox Code Playgroud)

和控制器有

    $scope.user = {};
    $scope.user.dob = new Date();
    $scope.maxDate = new Date(
       $scope.user.dob.getFullYear() - 10,
       $scope.user.dob.getMonth(),
       $scope.user.dob.getDate()
    );
    $scope.minDate = new Date(
       $scope.user.dob.getFullYear() - 120,
       $scope.user.dob.getMonth(),
       $scope.user.dob.getDate()
    );
Run Code Online (Sandbox Code Playgroud)

并且HTML是;

<md-datepicker 
 ng-model="user.dob" 
 md-placeholder="Enter date of birth"
 md-min-date="minDate" 
 md-max-date="maxDate">
</md-datepicker>
Run Code Online (Sandbox Code Playgroud)

使用此代码,该字段默认显示当前日期,我不想要,

我希望默认情况下日期字段为空.

此外,我想获得以下两种方式的价值:1)日期 - 月 - 年和2)日 - 月 - 年 - 小时 - 分 - 秒

当我试图得到它显示的值"09-11-2016T18:30:00.000Z"

我想要"09-11-2016"或"09-11-2016 18:30:00"

Mar*_*ben 5

您的mdDateLocaleProvider不会检查空值.

你的问题是:

app.config(['$mdDateLocaleProvider', function ($mdDateLocaleProvider) {
  $mdDateLocaleProvider.formatDate = function(date) {
     return moment(date).format('DD-MM-YYYY');
  }}]);
Run Code Online (Sandbox Code Playgroud)

它需要是这样的:

$mdDateLocaleProvider.formatDate = function(date) {
   var m = moment(date);
   return m.isValid()? m.format('DD-MM-YYYY') : '';
};
Run Code Online (Sandbox Code Playgroud)

然后你可以设置

$scope.user.dob=null;
Run Code Online (Sandbox Code Playgroud)

并得到一个空的Datepicker.