Angular 指定的日期值不符合要求的格式5

Angular 指定的日期值不符合要求的格式5,angular,typescript,angular5,Angular,Typescript,Angular5,在我的Angular应用程序中,我在输入字段中显示从API到组件的数据 正在填充所有字段,但不是类型为Date的输入元素 下面是html标记 <input [(ngModel)]="CustomerVM.customer.CustomerDob" type="date" name="MemberDateOfBirth" class="form-control" (blur)="Calculate_Age(CustomerVM.customer.CustomerDob)

在我的Angular应用程序中,我在输入字段中显示从API到组件的数据

正在填充所有字段,但不是类型为Date的输入元素

下面是html标记

<input [(ngModel)]="CustomerVM.customer.CustomerDob" type="date" name="MemberDateOfBirth" class="form-control" 
          (blur)="Calculate_Age(CustomerVM.customer.CustomerDob)">
}

但它不在日期字段中显示值

在插值块内,我们可以使用管道格式化值

{{DOB | date:'mediumDate'}}
我们也能用ngModel做这个吗?因为我不想要一个格式化它的方法


我如何才能做到这一点?

您的服务应该是这样的:

FormatDate(iDate: Date) {
   var inputDate = new Date(iDate);
   var formattedDate = inputDate.getFullYear()+'-'+(inputDate.getMonth() + 1)+'-'+ 
   inputDate.getDate();
   return formattedDate;
}
let newDate = new Date(this.CustomerVM.customer.CustomerDob);
this.CustomerVM.customer.CustomerDob = this.Helper.FormatDate(newDate);
在您的ts文件中,应该有如下内容:

FormatDate(iDate: Date) {
   var inputDate = new Date(iDate);
   var formattedDate = inputDate.getFullYear()+'-'+(inputDate.getMonth() + 1)+'-'+ 
   inputDate.getDate();
   return formattedDate;
}
let newDate = new Date(this.CustomerVM.customer.CustomerDob);
this.CustomerVM.customer.CustomerDob = this.Helper.FormatDate(newDate);

日期的响应是时间戳格式。时间戳值未填充日期字段,因此需要将时间戳格式转换为日期格式

var datePipe=new datePipe(“en-US”);
让formattedyear=datePipe.transform(this.CustomerVM.customer.CustomerDob,'MM/dd/yyyy');
this.CustomerVM.customer.CustomerDob=格式年


然后从“@angular/common”导入{DatePipe};在组件中,动作表达式不能包含管道。通过将
[(ngModel)]
提供的双向绑定拆分为属性绑定和事件绑定,可以实现更复杂的绑定。然后,日期管道可以包含在属性绑定表达式中:

<input [ngModel]="item.value | date:'yyyy-MM-dd'" (ngModelChange)="item.value=$event" type="data" />
使用ngValue


让我先试试这个,它实际上并不能解决问题。我收到了同样的警告,日期输入没有显示我设置的日期。你试过吗,我在这里对var formattedDate=新日期(inputDate.getFullYear(),(inputDate.getMonth()+1),inputDate.getDate())做了一些修改;在控制台中,它以所需格式打印值,但字段中的值仍然没有填充
<input [ngModel]="CustomerVM.customer.CustomerDob | date:'yyyy-MM-dd'" (ngModelChange)="CustomerVM.customer.CustomerDob=$event" type="date" name="MemberDateOfBirth" class="form-control" (blur)="Calculate_Age(CustomerVM.customer.CustomerDob)">