【问题标题】:How get input of date from ngb-datepicker in Angular?如何从 Angular 中的 ngb-datepicker 获取日期输入?
【发布时间】:2020-10-22 11:15:10
【问题描述】:

我创建了一个回历日历,我需要从用户那里获取输入并将其发送到后端,我的 HTML 代码:

  <div class="form-row ">
      <div class="form-group col-md-6 mb-4">
        <label class="w-100 d-block"> From </label>
        <mat-form-field class="w-100" matStartDate>
          <input matInput [min]="currentDate" name="dp"  (ngModelChange)="select(model)"  formControlName="DueDate" />
          <ngb-datepicker class="rtl"  [firstDayOfWeek]="7">
          </ngb-datepicker>
        </mat-form-field>
      </div>
      <div class="form-group col-md-6 mb-4">
        <label class="w-100 d-block">To</label>
        <mat-form-field class="w-100" matEndDate>
          <input matInput readonly=true [min]="minDate" name="dp" (ngModelChange)="select(model)" formControlName="ExpiryDate" [(ngModel)]="lastDate" />
          <ngb-datepicker class="rtl" #dp  [firstDayOfWeek]="7">
          </ngb-datepicker>

        </mat-form-field>
      </div>
    </div>

【问题讨论】:

  • Asma,您是否使用反应式表单 - 在输入 [formControl] 中使用或 formControlName- 或模板驱动表单 - 在输入 [(ngModel)] 中使用-?请记住,您不能在同一输入中同时使用两者。注意:该值是 formControl 的值 -if use ReactiveForms- 或变量的值 -if use [(ngModel)],但请记住,在发送到您的您需要传递给日期对象或字符串的 dbs
  • 感谢 Eliseo,我使用了响应式表单,但在将其发送到数据库之前,我需要将其保存在变量中并进行一些连接。我该怎么做?
  • 创建一个函数将 NgbDateStructure 转换为字符串(或日期)。确实完整的回复有点大,希望对你有帮助

标签: angular datepicker calendar hijri


【解决方案1】:

您需要创建一个函数来将 NgbDateStructure 转换为您在 dbs 中使用的数据类型

通常您向数据库发送 ISO 格式 yyyy-MM-dd 的字符串

ngbDateToString(date:any)
{
    return date.year+'-'+('0'+date.month).slice(-2)+('0'+date.day).slice(-2)
}

或者是使用日期对象

ngbDateToDate(date:any)
{
    return new Date(date.year+'-'+('0'+date.month).slice(-2)+('0'+date.day).slice(-2))
}

但我看到一些混淆了您的代码。我想您使用服务获取数据。我喜欢从 http.client 获取数据时,在 Date 对象中转换 de props 类型 Date - 通常您将类型 date 作为字符串接收,因此,在服务中使用 map 来转换数据

//declare a interfacte in the service
export DataInterface
{
  DueDate:Date|string
  ExpiryDate:Date|string
  ... rest of properties..
}

getData(id):Observable<DataInterface>
{
   //generally, you received DueDate or ExpiryDate as string, so we not use only
   //return this.httpClient.get('....') as DataInterface; 

   //else transform the string to Date
    return this.httpClient.get('....').pipe(
    map(res=>{
      res.DueDate=new Date(res.DueDate)
      res.ExpiryDate=new Date(res.ExpiryDate)
      return res;
    }
    ))
}

如果您有获取对象列表的函数,则同理

getList(id):Observable<DataInterface[]>
{
    return this.httpClient.get('....').pipe(
    map(res=>{
      res.forEach(x=>{
        x.DueDate=new Date(x.DueDate)
        x.ExpiryDate=new Date(x.ExpiryDate)
      }
      return res;
    }
    ))
}

但是 ngb-datepicker 使用 NgbDateStructur,所以你需要在接收数据和发送数据时进行转换。通常你在组件中有一个函数 createForm

createForm(data:DataInterface=null)
{
    data=data || {} as DataInterface
    return new FormGroup({
        DueDate:new FormControl(data.DueDate?
             {
               year:data.DueDate.getFullYear(),
               month:data.DueDate.getMonth()+1,
               day:data.DueDate.getDate()
             }:
             null),
        ExpiryDate:new FormControl(data.ExpiryDate?
             {
               year:data.ExpiryDate.getFullYear(),
               month:data.ExpiryDate.getMonth()+1,
               day:data.ExpiryDate.getDate()
             }:
             null),
         ..rest of FormControl, e.g...
         id:new FormControl(data.id);
    })
}

现在你有一个表格。看到只使用formControlName

<form [formGroup]="form" (submit)="submit(form)">
      <div>
         <input class="form-control" placeholder="yyyy-mm-dd"
             name="dp" formControlName="DueDate" ngbDatepicker 
             class="rtl"  [firstDayOfWeek]="7"
             #dDueDate="ngbDatepicker">
          <button class="btn btn-outline-secondary calendar" (click)="dDueDate.toggle()" 
             type="button"></button>
      </div>

      <div>
         <input class="form-control" placeholder="yyyy-mm-dd"
             name="do" formControlName="ExpiryDate" ngbDatepicker 
             class="rtl"  [firstDayOfWeek]="7"
             #dExpiryDate="ngbDatepicker">
          <button class="btn btn-outline-secondary calendar" (click)="ExpiryDate.toggle()" 
             type="button"></button>
      </div>
      ..rest of FormControls in the way, e.g...
      <input formControlName="id">
      .....
      <button type="submit">submit</submit>
</form>

在您的 component.ts 中

//in ngOnInit()
ngOnInit()
{
    ..get the id from params or whatever
    this.dataService.getData(id).subscribe(res=>{
         this.form=this.createForm(res);
    })
}

在提交中我们可以先使用相关的功能

submit(form:FormGroup)
{
   if (form.valid)
   {
       const data={...form.value} //make a copy
       data.DueDate=this.ngbDateToString(data.DueDate)
       data.ExpiryDate=this.ngbDateToString(data.ExpiryDate)
       this.dataService.updateData(data).subscribe(res=>{
           console.log(res)
       })
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2019-12-22
    相关资源
    最近更新 更多