【问题标题】:ionic input date set default to today离子输入日期设置默认为今天
【发布时间】:2016-03-10 08:50:16
【问题描述】:

我正在开发一个 Ionic 应用程序。我正在尝试在输入日期上设置默认日期(今天)。

如果输入类型是文本,我的代码就可以工作。但是如果我定义一个日期类型,它就不起作用了。

HTML 代码:

<html ng-app="ionicApp">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

  <title>Date with Ionic</title>

  <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">

  <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>

</head>

<body ng-controller="GraphCtrl">

  <ion-header-bar class="bar-positive">
    <h1 class="title">Ionic Date</h1>
  </ion-header-bar>

  <ion-content>
    Text : <input type="text" ng-model="date_rdv" />
    Date : <input type="date" ng-model="date_rdv" />
  </ion-content>

</body>
</html>

我的 JS 代码:

angular.module('ionicApp', ['ionic'])

.controller('GraphCtrl', function($scope, $filter) {
  $scope.date_rdv = $filter('date')(Date.now(), 'yyyy-MM-dd');
});

你有什么想法吗?

Codepen.io link

解决方案(根据 Elec 的回答)

为了在日期输入上定义一个默认值(对文本输入不起作用),模型的定义必须如下:

$scope.date_rdv = new Date();

【问题讨论】:

    标签: javascript angularjs ionic-framework ionic


    【解决方案1】:

    您应该改用new Date()。你不需要过滤。

    模型必须始终是 Date 对象,否则 Angular 会抛出错误。无效的 Date 对象(getTime() 为 NaN 的日期)将呈现为空字符串。

    AngularJs documentation for input[data]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-13
      • 2011-10-22
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多