我正在尝试加载作为时间戳存储在Firestore中的字段,并填充一个使用Date格式的“ mat-datepicker”表单控件。
在到达“ Angular Material”日期选择器控件之前,有什么方法可以修改“ dob”数据的格式?日期选择器不喜欢时间戳格式,并且出于明显的原因拒绝显示它。
以下是我缩短现有代码的尝试减去任何转换尝试,因为我被卡住了!可能甚至行不通!完整的代码可以。
基本上使用传入的路径字符串创建Firestore文档的实例,然后将值修补到myForm控件中。
我知道firebase.firestore.Timestamp具有toDate()选项,但我是Angular菜鸟,在将值存储在控件中之前,需要一些关于如何将时间戳转换为日期的指针吗?
// Firestore Document
private fireDoc: AngularFirestoreDocument;
@Input() path: string;
myForm: FormGroup;
constructor(private fb: FormBuilder, private afs: AngularFirestore) { }
ngOnInit() {
this.myForm = this.fb.group({
firstName: new FormControl(''),
middleName: new FormControl(''),
lastName: new FormControl(''),
dob: new FormControl(),
});
this.loadDoc();
}
loadDoc() {
this.fireDoc = this.afs.doc(this.path);
this.fireDoc
.valueChanges()
.pipe(
tap(doc => {
if (doc) {
this.myForm.patchValue(doc);
}
}),
take(1)
)
.subscribe();
}
}
我在angularfirebase.slack.com上问了这个问题,并获得了此解决方案。
pipe(
map(({dob, ...data}) => {
return {
...data,
dob: dob.toDate(),
};
}),
点击(...)>
在我的场景中,我在消防站有5个时间戳,并且撞到了砖墙。上面的代码对我有用。