【问题标题】:Mat-DatePicker - [(ngModel)] - not able to show user saved dateMat-DatePicker - [(ngModel)] - 无法显示用户保存的日期
【发布时间】:2019-04-10 06:07:13
【问题描述】:

有人能告诉我如何在 Mat-Datepicker 中显示用户保存的日期吗?

最初从 get 服务获取 endDate 为 null(来自嵌套对象的数据,从另一个组件调用)。进入编辑模式时。正在选择值并保存它。在控制台中,它打印选定的日期。但在网络中它变为空。

this.endDate: string;

<mat-form-field>
<input matInput [value] = "endDate" [matDatepicker]="myDatepicker"  name="endDate" [(ngModel)]="endDate">
<mat-datepicker-toggle matSuffix [for]="myDatepicker"></mat-datepicker-toggle>
<mat-datepicker #myDatepicker ></mat-datepicker>
</mat-form-field>

在保存方法中:

save(){
let obj = {
    endDate = this.endDate; // my ngModel
    }
}
updateChange(d){
this.endDate = d; // i can see the changed value here but not able to save
} 

保存数据的实际对象

const sample : OriginalModel = { 
demoKey : this.sample['key'].demo, 
demoKey2 : this.sample['key2'].demo2, 
endDate : this.endDate 
}

如何在编辑模式下显示用户保存的值?

请分享解决方案/工作堆栈闪电战。

提前致谢

【问题讨论】:

  • 你为什么要这样包装你的对象?
  • 好吧实际上是用于示例目的的对象的一部分。我的对象会像const sample : OriginalModel = { demoKey : this.sample['key'].demo, demoKey2 : this.sample['key2'].demo2, endDate : this.endDate }

标签: angular typescript datepicker angular-material


【解决方案1】:

我不知道您为什么要尝试将日期值包装到 {} 但您可以使用以下语法来实现这一点,

您的组件方面,

sample: any = { demoKey: "", demoKey2: "", demoKey3: "", endDate: new Date }

在您的 html 方面,

<mat-form-field>
<input  matInput placeholder="demoKey1" name='demoKey' [(ngModel)]='sample.demoKey' >
...
</mat-form-field>

你不需要使用保存函数来设置两种方式绑定变量,尝试用一个函数来控制台日志obj.endDate,看看会发生什么神奇的事情。

Here isstackblitz 示例,希望对您有所帮助。

【讨论】:

  • 请检查我最后的评论。这就是我保存数据的实际 obj 结构
  • 你能不能制作 stackblitz .. 我试过你的解决方案。不适合我
  • 好的。给我一些时间。
  • 当然..我也在尝试。 :)
  • 你的 stackblitz 没有正确加载。显示空白页
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-20
  • 1970-01-01
  • 2019-12-19
  • 2018-06-23
  • 1970-01-01
相关资源
最近更新 更多