我的应用程序中有两个输入,我试图将它们组合到一个对象中(因为服务器期望一个对象)

以下模板:

<ion-item>
            
            <mat-form-field>
                <mat-label>Data Appuntamento</mat-label>
                <input matInput [matDatepicker]="picker" [(ngModel)]="this.DataPrenotazione" >
                <mat-hint>DD/MM/YYYY</mat-hint>
                <mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
                <mat-datepicker #picker></mat-datepicker>
            </mat-form-field>
            <ion-label>Ora Appuntamento</ion-label>
            <ngx-timepicker-field [format]="24" [(ngModel)]="this.OraPrenotazione" [minutesGap]="15"></ngx-timepicker-field>
            
        </ion-item>

然后我称其为组合输入:

combineDateTime(date: Date, time: Date): Date {
        const combinedDateTime = new Date();
        combinedDateTime.setFullYear(date.getFullYear());
        combinedDateTime.setMonth(date.getMonth());
        combinedDateTime.setDate(date.getDate());
        combinedDateTime.setHours(time.getHours());
        combinedDateTime.setMinutes(time.getMinutes());
        return combinedDateTime;
    }

当我try 调用组合函数时,我得到".getFullYear()不是一个函数"

this.combineDateTime(this.DataPrenotazione, this.OraPrenotazione)

推荐答案

我们从时间 Select 器获得的输出为02:30,因此我们需要相应地分割它.我们可以在:上使用字符串拆分,然后使用一元+将拆分的字符串转换为数字,最后设置小时和分钟!

  combineDateTime(date: Date, time: string): Date {
    const combinedDateTime = new Date();
    combinedDateTime.setFullYear(date.getFullYear());
    combinedDateTime.setMonth(date.getMonth());
    combinedDateTime.setDate(date.getDate());
    const [hours, minutes] = time.includes(':') ? time.split(':') : [0, 0];
    combinedDateTime.setHours(+hours);
    combinedDateTime.setMinutes(+minutes);
    return combinedDateTime;
  }

完整代码:完整代码:

TS :

import { Component } from '@angular/core';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
import { provideNativeDateAdapter } from '@angular/material/core';
import { FormsModule } from '@angular/forms';
import { NgxMaterialTimepickerModule } from 'ngx-material-timepicker';
import { CommonModule } from '@angular/common';

/** @title Basic datepicker */
@Component({
  selector: 'datepicker-overview-example',
  templateUrl: 'datepicker-overview-example.html',
  standalone: true,
  providers: [provideNativeDateAdapter()],
  imports: [
    MatFormFieldModule,
    MatInputModule,
    MatDatepickerModule,
    FormsModule,
    NgxMaterialTimepickerModule,
    CommonModule,
  ],
})
export class DatepickerOverviewExample {
  OraPrenotazione: string = '00:00';
  DataPrenotazione: Date = new Date();
  combinedDateTime: any = null;

  combineDateTime(date: Date, time: string): Date {
    const combinedDateTime = new Date();
    combinedDateTime.setFullYear(date.getFullYear());
    combinedDateTime.setMonth(date.getMonth());
    combinedDateTime.setDate(date.getDate());
    const [hours, minutes] = time.includes(':') ? time.split(':') : [0, 0];
    combinedDateTime.setHours(+hours);
    combinedDateTime.setMinutes(+minutes);
    return combinedDateTime;
  }
}

HTML:

<mat-form-field>
  <mat-label>Data Appuntamento</mat-label>
  <input
    matInput
    [matDatepicker]="picker"
    [(ngModel)]="this.DataPrenotazione"
  />
  <mat-hint>DD/MM/YYYY</mat-hint>
  <mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>
<label>Ora Appuntamento</label>
<ngx-timepicker-field
  [format]="24"
  [(ngModel)]="this.OraPrenotazione"
  [minutesGap]="15"
></ngx-timepicker-field>

<button
  (click)="combinedDateTime = combineDateTime(DataPrenotazione, OraPrenotazione)"
>
  combine
</button>

Final DATE: {{combinedDateTime | date : 'full'}}

Stackblitz Demo

Angular相关问答推荐

Angular 混合应用程序-模型未更新 Select

如何并行执行多个API调用并在响应到达Angular中的HTTP服务时处理响应?

如何避免使用CSS动画制作的幻灯片中闪烁?

使Angular 效果只执行一次

如何在自定义验证器中获取所有表单控件(字段组动态创建)

无法绑定到appendTo,因为它不是p-confirmPopup的已知属性

如何使用formBuilder.Group()从角形表单中删除选定的选项?

获取 Angular 中所有子集合 firestore 的列表

Router.navigate() 在功能拦截器内不起作用

Angular 15 在 URL 中使用@进行路由

在指令中获取宿主组件的templateRef

Nz 树 Select .如何在子 node 中显示父 node 名称?

在父组件的子组件中使用 ng-template

如何以Angular 显示动态视图模型

个人修改 Angular CLI - 创建新应用

当父组件上的变量发生变化时重新加载子组件

如何以Angular 获取嵌套表单组的控件

如何添加/设置环境 Angular 6 angular.json 文件

在 Angular 2 中使用 store (ngrx) 有什么好处

Angular 2 Material 2 日期 Select 器日期格式