【发布时间】:2018-08-05 06:05:12
【问题描述】:
我是角度和材料 2 的新手,我在表单中使用材料日期选择器来显示和选择日期。我可以选择日期并绑定到 ngModel。但是当我尝试将我的响应绑定到 ngModel 时,什么也没显示。我已将字符串日期转换为日期,然后绑定到 ngModel。以下是代码,请帮助我在哪里丢失。
<mat-form-field>
<input matInput id="dateOfBirth" name="dateOfBirth" [matDatepicker]="dateOfBirth"
class="form-control" [(ngModel)]="customer.dateOfBirth"
placeholder="Date of birth" required>
<mat-datepicker-toggle matSuffix [for]="dateOfBirth"></mat-datepicker-toggle>
<mat-datepicker #dateOfBirth></mat-datepicker>
<mat-error>Date of birth is required</mat-error>
</mat-form-field>
我的 ts 文件从 json 获取响应
customer: CustomerModel = new CustomerModel();
this.customerService.getResponse().subscribe(resp => {
this.customer= resp;
console.log('Date:'+this.customer.dateOfBirth);// able to print
});
Class CustomerModel {
dateOfBirth: Date;
//other customer details
}
【问题讨论】:
-
您是否在控制台中看到错误消息?
-
你能尝试在 stackblitz 上提供你的代码吗?
-
@ConnorsFan:不,我没有看到任何错误
-
@davecar21: stackblitz.com/edit/angular-pvqugs