我正在使用
我想要实现的目标
操纵文档数据
将返回的日期转换为可读的内容
我有什么
我有一个相册列表,我使用 snapshotChanges 来返回映射数据
我的专辑是 AngularFirestorDocument
问题
相册列表
所以这个组件正确地带回了一个专辑列表,我可以在返回它之前操纵它,以便HTML显示正确的格式。
export class AlbumsListCompoment implements OnInit {
private albumCollection: AngularFirestoreCollection<any>;
albumns: Observable<any[]>;
folderId: string;
constructor(
private readonly afs: AngularFirestore,
private activatedRoute: ActivatedRoute,
private router: Router
) {
// Look at the url for the Folder ID and set the local variable
this.activatedRoute.params.forEach((urlParameters) => {
this.folderId = urlParameters['folderId'];
});
// Album Reference "folders/folderid/albums"
this.albumCollection = afs.collection<any>(`folders/${this.folderId}/albums`);
// Get the data
this.albumns = this.albumCollection.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data();
// Get the date from each album
var albumDateTimeStapm = data.album_date;
// Convert the unix value and format it based on the users locale setting
var albumDateISO = moment(albumDateTimeStapm).format("DD/MM/YYYY");
// Create a new 'name' to use in the HTML binding
data.formattedDate = albumDateISO;
const id = a.payload.doc.id;
return { id, ...data };
});
});
}
ngOnInit() {
}
}
相册
我并未完全展示如何处理此组件以执行相同的操作。它不是一个列表,它是一个单独的文档/专辑。
export class AlbumDetails implements OnInit {
folderId: string;
albumId: string;
private albumDoc: AngularFirestoreDocument<any>;
album: Observable<any>;
constructor(
private readonly afs: AngularFirestore,
private activatedRoute: ActivatedRoute,
private router: Router) {
// Look at the url for the Folder and Album ID and set the local variable
this.activatedRoute.params.forEach((urlParameters) => {
this.folderId = urlParameters['folderId'];
this.albumId = urlParameters['albumId'];
});
this.albumDoc = afs.doc<any>(`folders/${this.folderId}/albums/${this.albumId}`);
this.album = this.albumDoc.valueChanges();
}
ngOnInit() {}
}
非常感谢任何帮助:)
答案 0 :(得分:1)
我不是100%确定我理解你的问题。根据我的理解,您希望处理文档的返回数据,然后在显示HTML之前更改它。
如果是这种情况,您可以执行以下操作:
this.albumDoc = afs.doc<any>(`folders/${this.folderId}/albums/${this.albumId}`);
this.album = this.albumDoc.valueChanges();
this.album.subscribe(value => {
const value1 = value.value1;
const value2 = value.value2;
...
const valueN = value.valueN;
});
如果您还需要返回值的元数据(例如id),请执行以下操作:
this.albumDoc = afs.doc<any>
(`folders/${this.folderId}/albums/${this.albumId}`);
this.album = this.albumDoc.snapshotChanges();
this.album.subscribe(value => {
const id = value.payload.id;
const value1 = value.payload.data().value1;
const value2 = value.payload.data().value2;
...
const valueN = value.payload.data().valueN;
});
有关详细信息,请参阅angularfire2 documentation