日期输入字段中模型绑定日期值的角度显示值

发布于 2025-01-10 18:47:52 字数 2104 浏览 0 评论 0原文

我将此模型传递给视图:

export class ProgramModel {
  programId: bigint;
  category = new CategoryModel();
  name: string;
  description: string;
  shortDescription: string;
  support: string [];
  address: string;
  location: [];
  areaServed: string;
  baseLocation: string;
  website: string;
  topic: string [];
  ownerLid: string;
  keywords: string[] = [];
  startDate: Date;
  endDate: Date;
  notification: boolean;
  daysForNotice: number;
  filter: string [];
  active: boolean;
  approved: boolean;
  status: string;
  contact: ContactModel[] = [];
  departmentContact: ContactModel[] = [];
}

startDateendDate 的呈现方式如下:

    <div class="row">
      <div class="col">
        <div class="form-group">
          <label for="startDate">Start Date</label>
          <input style="line-height: 20px" type="date" class="form-control" id="startDate"
                 [(ngModel)]="model.startDate" name="date">
        </div>
      </div>
      <div class="col">
        <div class="form-group">
          <label for="endDate">End Date</label>
          <input style="line-height: 20px" type="date" class="form-control" id="endDate"
                 [(ngModel)]="model.endDate" name="date">
        </div>
      </div>
    </div>

使用此方法简单地创建一个新的数据库记录就可以了,但是可以重用该代码编辑页面我发现我无法让这些日期输入显示先前选择的值,就像我可以使用其他字段一样。

输入图片此处描述

这是页面加载时开始和结束日期的值: 开始日期:2022 年 1 月 29 日星期六 19:00:00 GMT-0500(东部标准时间) endDate: Fri Mar 11 2022 19:00:00 GMT-0500(东部标准时间)

这就是我将它们变成日期的地方:

this.programService.getProgramByID(item.programId).subscribe(
      (theProgram) => {
        theProgram.startDate = new Date(theProgram.startDate);
        theProgram.endDate = new Date(theProgram.endDate);
        this.model = theProgram;
      });

如何使这些日期输入显示值?

I am passing this model to the view:

export class ProgramModel {
  programId: bigint;
  category = new CategoryModel();
  name: string;
  description: string;
  shortDescription: string;
  support: string [];
  address: string;
  location: [];
  areaServed: string;
  baseLocation: string;
  website: string;
  topic: string [];
  ownerLid: string;
  keywords: string[] = [];
  startDate: Date;
  endDate: Date;
  notification: boolean;
  daysForNotice: number;
  filter: string [];
  active: boolean;
  approved: boolean;
  status: string;
  contact: ContactModel[] = [];
  departmentContact: ContactModel[] = [];
}

The values startDate and endDate are presented like this:

    <div class="row">
      <div class="col">
        <div class="form-group">
          <label for="startDate">Start Date</label>
          <input style="line-height: 20px" type="date" class="form-control" id="startDate"
                 [(ngModel)]="model.startDate" name="date">
        </div>
      </div>
      <div class="col">
        <div class="form-group">
          <label for="endDate">End Date</label>
          <input style="line-height: 20px" type="date" class="form-control" id="endDate"
                 [(ngModel)]="model.endDate" name="date">
        </div>
      </div>
    </div>

Simply creating a new db record using this works fine, but reusing the code for the edit page I have found that I can't get those date input's to display the previously selected value like I can with the other fields.

enter image description here

This is the value of start and end date when the page loads:
startDate: Sat Jan 29 2022 19:00:00 GMT-0500 (Eastern Standard Time)
endDate: Fri Mar 11 2022 19:00:00 GMT-0500 (Eastern Standard Time)

And this is where I make them into a date:

this.programService.getProgramByID(item.programId).subscribe(
      (theProgram) => {
        theProgram.startDate = new Date(theProgram.startDate);
        theProgram.endDate = new Date(theProgram.endDate);
        this.model = theProgram;
      });

How can I make those date input's display the values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文