简体   繁体   English

如何显示日期<input type="date">使用 Angular 时的字段

[英]How to display a date in <input type = "date"> field when using Angular

I want to get some inputs values from the user by using a form and save it in the database.我想通过使用表单从用户那里获取一些输入值并将其保存在数据库中。 Then I want to display the saved data using this same form.然后我想用同样的形式显示保存的数据。 But even though I get the values from these input fields I can not display the retrieved values here;但是即使我从这些输入字段中获取值,我也无法在此处显示检索到的值;

<input type="date" name="ArrivalDate" #ArrivalDate="ngModel" [(ngModel)]="service.formData.ArrivalDate" class="form-control">

And

<input type="date" name="DepartureDate" #DepartureDate="ngModel" [(ngModel)]="service.formData.DepartureDate" class="form-control">

Screenshot: Display retrieved data from the database but date fields are not filled with the necessary data屏幕截图:显示从数据库中检索到的数据,但日期字段未填充必要的数据

/ ----- code segment ----- / / ----- 代码段 ----- /

<div class="form-row">
            <div class="form-group col-md-4">
              <div class="form-group">
                  <label>Arrival Date</label>
                  <!--I want to get the date input from the user-->
                  <!--I want to display the date input which given by the user-->
                  <input type="date" name="ArrivalDate" #ArrivalDate="ngModel" [(ngModel)]="service.formData.ArrivalDate" class="form-control">
                  <div class="validation-error" *ngIf="ArrivalDate.invalid && ArrivalDate.touched">This field is required.</div>
              </div>
            </div>
            <div class="form-group col-md-4">
              <div class="form-group">
                  <label>Departure Date</label>
                   <!--I want to get the date input from the user-->
                  <!--I want to display the date input which given by the user-->
                  <input type="date" name="DepartureDate" #DepartureDate="ngModel" [(ngModel)]="service.formData.DepartureDate" class="form-control">
                  <div class="validation-error" *ngIf="DepartureDate.invalid && DepartureDate.touched">This field is required.</div>
              </div>
            </div>
            <div class="form-group col-md-4">
                <div class="form-group">
                  <label>Star Category</label>
                  <input name="StarCategory" #StarCategory="ngModel" [(ngModel)]="service.formData.StarCategory" class="form-control">
                  <div class="validation-error" *ngIf="StarCategory.invalid && StarCategory.touched">This field is required.</div>
              </div>
            </div>
        </div>

You can try like this.你可以这样试试。 This is a minimal form with 2 date field.这是具有 2 个日期字段的最小形式。 You can add the rest of the fields.您可以添加字段的 rest。

in component template在组件模板中

<form [formGroup]="dateForm" (ngSubmit)="onSubmit()">

  <label>
    Arrival Date:
    <input type="date" formControlName="ArrivalDate">
  </label>

  <label>
    Departure Date:
    <input type="date" formControlName="DepartureDate">
  </label>
  <input type="submit" />
</form>

<label> Arrival Date: {{dateForm.get("ArrivalDate").value}}</label>

<label> Departure Date: {{dateForm.get("DepartureDate").value}}</label>

in component在组件中

  import { FormGroup, FormControl } from "@angular/forms";

  arrivalDate : Date = new Date();
  departureDate : Date = new Date();
  dateForm = new FormGroup({
    ArrivalDate: new FormControl(this.arrivalDate.toISOString().split("T")[0]),
    DepartureDate: new FormControl(this.departureDate.toISOString().split("T")[0])
  });

  onSubmit(){
    console.log(this.dateForm.value);
  }

working sample in stackblitz: https://stackblitz.com/edit/angular-xkgxxt stackblitz 中的工作样本: https://stackblitz.com/edit/angular-xkgxxt

The issue is that the default datepicker of the browser can display date strings (not date objects) with the date section only (without the time).问题是浏览器的默认日期选择器只能显示带有日期部分的日期字符串(不是日期对象)(没有时间)。

You need to convert your dates to strings first, like in this example: https://stackblitz.com/edit/angular-zpntay?file=src/app/hello.component.ts您需要先将日期转换为字符串,如下例所示: https://stackblitz.com/edit/angular-zpntay?file=src/app/hello.component.ts

@Component({
  selector: 'hello',
  template: `
  <form [formGroup]="myForm">
    <input type="date" formControlName="dateOfBirth">
  </form>`,
})
export class HelloComponent implements OnInit  {
  myForm: FormGroup;

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.myForm = this.fb.group({
      dateOfBirth: new Date('1988-05-15').toISOString().split('T')[0]
    });
  }
}

I changed;我变了; <input type="date"> to <input type="datetime"> <input type="date"><input type="datetime">

Then I could successfully display the date and time.然后我可以成功显示日期和时间。

Issue: When I do the insertion, arrival date & departure date stored in the database like this.问题:当我像这样存储在数据库中的插入、到达日期和离开日期时。 '2020-05-01T00:00:00' When I retrieved data I used to display it using <input = "date"> field, so I changed it to <input type= "datetime"> '2020-05-01T00:00:00' 当我检索数据时,我曾经使用<input = "date">字段显示它,所以我将其更改为<input type= "datetime">

          <input class="form-control" type="date" (change)="SendDatetoFunction($event)" name="date" id="date">

#function in angular ts file #angular ts文件中的函数

SendDatetoFunction(event: any) {
    console.log(event.target.value);
  }

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

相关问题 如何在Angular JS的输入类型日期中显示日期? - How to display a Date in input type date in angular js? 如何以角度设置当前日期的输入字段 - How to set input field with current date in angular 如何验证输入 type=date 字段并显示错误消息,无论该字段为空还是与正则表达式不匹配 - How to validate an input type=date field and display an error messages whether the field is empty or if it does not match the regex 当我从中选择日期时,如何打印日期 <input type=“date” > 使用AJAX? - How can i print the date ,when i select the date from <input type=“date” > using AJAX? 如何在Web中显示具有日期/时间类型的字段的日期选择器 - How to display date picker for field with date/time type in web 角度:在日期字段的输入类型文本中添加斜杠“ /” - Angular: add slash “/” in the input type text for date field 输入字段中日期类型为中心的日期 - Center date in input field with date type asp输入字段类型日期 - asp input field type date 如何根据用户在 startdate 字段中选择的日期禁用以前的日期(在 enddate 字段中)? (使用输入类型“日期”和 javascript) - How to disable previous dates(in the enddate field) based on the date the user selected on the startdate field? (using input type "date" & javascript) 如何使用angular在HTML中显示日期? - How to display Date in Html using angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM