简体   繁体   English

离子输入日期默认设置为今天

[英]ionic input date set default to today

I'm working on an Ionic application. 我正在开发离子应用程序。 I'm trying to set a default date (today) on an input date. 我正在尝试在输入日期上设置默认日期(今天)。

My code works if the type of input is text . 如果输入类型是text,则我的代码有效。 But it doesn't work if I define an date type . 但是,如果我定义日期类型的话,那是行不通的。

HTML code : 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>

My JS code : 我的JS代码:

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

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

Do you have any idea ? 你有什么主意吗 ?

Codepen.io link Codepen.io链接

Solution (according to Elec's answer) 解决方案(根据Elec的回答)

In order to define a defaultvalue on date input (not functionnal with text input), the definition of model must be as following : 为了在日期输入中定义默认值(对于文本输入不起作用),模型的定义必须如下:

$scope.date_rdv = new Date();

you should use new Date() instead. 您应该改用new Date() you don't need to filter. 您不需要过滤。

The model must always be a Date object, otherwise Angular will throw an error. 该模型必须始终是Date对象,否则Angular将引发错误。 Invalid Date objects (dates whose getTime() is NaN) will be rendered as an empty string. 无效的Date对象(getTime()为NaN的日期)将呈现为空字符串。

AngularJs documentation for input[data] AngularJs文档输入[数据]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM