【问题标题】:Bind array to `datetime-local`?将数组绑定到`datetime-local`?
【发布时间】:2021-11-01 09:53:16
【问题描述】:

我有一项服务,它提取表示为数组 (e.g. [2021, 09, 02, 08, 46] or [yyyy, mm, dd, hh, mm]) 的日期时间

在我的组件中,以下属性存储上面提取的日期时间

public startTime : number[];

在我的模板中,我想将此数组绑定到我的输入,如下所示:

<input type="datetime-local" [ngModel]="startTime" />

有没有一种干净的方法将startTime(表示年/月/日/小时/时间作为数组)绑定到datetime-local?如果不需要的话,我不想做一堆强制转换或转换。

【问题讨论】:

    标签: angular typescript binding


    【解决方案1】:

    我想你可能需要一些演员表。

    const startTime = new Date(...startTimeArray);
    

    使用Date 对象,您可以随心所欲地使用它:

    <input type="datetime-local" [ngModel]="startTime | date:'yyyy-MM-ddTHH:mm'" (ngModelChange)="startTime = parseDateEvent($event)"/>
    
    parseDateEvent(date: string) {
       return !!date ? new Date(date) : null;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-14
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      相关资源
      最近更新 更多