Ionic 2 ion-datetime with displayFormat =" MMMM"没有改变模型值 [英] Ionic 2 ion-datetime with displayFormat ="MMMM" is not changing the model value
问题描述
我使用 ion-datetime
作为月份和年份下拉值。当我在 displayFormat
中放入 MMMM
时,它不会更改模型值。但是,如果我将 YYYY
放在 displayFormat
中,它的效果非常好。以下是我的HTML代码:
I am using ion-datetime
for the month and year dropdown values. When I put MMMM
in displayFormat
, it doesn't change the model value. However, if I put the YYYY
in displayFormat
, it works perfectly fine. Following is my HTML code:
<ion-item>
<ion-label>Month</ion-label>
<ion-datetime displayFormat="MMMM" [(ngModel)]="filter_data.month"></ion-datetime>
</ion-item>
<ion-item>
<ion-label>Year</ion-label>
<ion-datetime displayFormat="YYYY" [(ngModel)]="filter_data.year"></ion-datetime>
</ion-item>
我做错了什么?任何帮助将不胜感激。
What I am doing wrong? Any help would be appreciated.
推荐答案
根据日期时间|离子API文档,
同样重要的是要注意
displayFormat
或pickerFormat
可以设置日期时间值的输出,该值是组件的ngModel
设置的值。格式仅用于将值显示为文本和选择器的界面,但日期时间的值始终保持为有效的ISO 8601日期时间字符串。
It's also important to note that neither the
displayFormat
orpickerFormat
can set the datetime value's output, which is the value that is set by the component'sngModel
. The format's are merely for displaying the value as text and the picker's interface, but the datetime's value is always persisted as a valid ISO 8601 datetime string.
因此,ISO 8601中提供了这一点( W3 docs ),这里只允许使用以下格式的组合:
So as provided in the ISO 8601 (W3 docs), only combination leading to following formats would be allowed here:
- 年份:
YYYY
(例如1997年) - 年和月:
YYYY-MM
(例如1997-07) - 完成日期:
YYYY-MM-DD
(例如1997-07-16) - 完成日期加上小时和分钟:
YYYY- MM-DDThh:mmTZD
(例如1997-07-16T19:20 + 01:00) - 完成日期加上小时,分钟和秒:
YYYY-MM-DDThh:mm:ssTZD
(例如1997-07-16T19:20:30 + 01:00) - 完成日期p lus小时,分钟,秒和
秒的小数部分
YYYY-MM-DDThh:mm:ss.sTZD
(例如1997-07-16T19:20:30.45 + 01:00)
- Year:
YYYY
(eg 1997) - Year and month:
YYYY-MM
(eg 1997-07) - Complete date:
YYYY-MM-DD
(eg 1997-07-16) - Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD
(eg 1997-07-16T19:20+01:00) - Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD
(eg 1997-07-16T19:20:30+01:00) - Complete date plus hours, minutes, seconds and a decimal fraction of a
second
YYYY-MM-DDThh:mm:ss.sTZD
(eg 1997-07-16T19:20:30.45+01:00)
这解释了它无法使用的行为只需 MMMM
。
This explains the behaviour of it not working with just MMMM
.
编辑:
这意味着不允许使用 MMMM
,因此错误。当您尝试为 ion-datetime
的 ngModel
选择 MMMM <的值时/ code>格式,它会在控制台中给出以下警告:
This means it will not be allowed to use MMMM
and hence the error. When you try to select a value for an ion-datetime
's ngModel
having MMMM
format, it would give following warning in the console:
解析日期时出错: [object Object]。请提供有效的ISO 8601日期时间格式:
https:// www。 w3.org/TR/NOTE-datetime
这是示例plunker 说明这一点。
这篇关于Ionic 2 ion-datetime with displayFormat =" MMMM"没有改变模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!