【问题标题】:Why is the format 'HH:mm' not working in this DatePipe?为什么格式 'HH:mm' 在此 DatePipe 中不起作用?
【发布时间】:2018-05-14 14:50:31
【问题描述】:

我正在尝试在 input 字段中显示 Date 对象并且只显示分钟

<mat-form-field>
  <input matInput type="time" placeholder="Choose a time" 
       [(ngModel)]="appointmentTime"
       [ngModel]="appointmentTime  | date:'HH:mm'"/>
</mat-form-field>

但是,时间没有正确显示 - 事实上根本没有:

在类中,appointmentTimeappointmentDate被初始化为

public appointmentDate = new Date();
public appointmentTime = new Date();

【问题讨论】:

  • 您是否将管道注册为提供者? ngModel 中是否缺少圆括号?

标签: angular angular5


【解决方案1】:

您不必使用两次 ngModel,这应该可以工作,

<mat-form-field>
  <input matInput type="time" placeholder="Choose a time" 
       [ngModel]="appointmentTime  | date:'HH:mm'"/>
</mat-form-field>

STACKBLITZ DEMO

【讨论】:

  • Dangit .. 这确实是问题所在。非常感谢!
【解决方案2】:
 <input matInput type="time" placeholder="Choose a time" 
       [ngModel]="appointmentTime  | date:'HH:mm'"/>

【讨论】:

  • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation 将通过展示为什么这是解决问题的好方法,并使其对有其他类似问题的未来读者更有用,从而大大提高其长期价值。请edit您的回答添加一些解释,包括您所做的假设。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 2016-05-17
  • 2013-01-10
  • 2020-11-28
  • 2012-08-25
  • 1970-01-01
相关资源
最近更新 更多