Ionic 2 使用 AngularFire2 列表到 ts 文件中
Ionic 2 use AngularFire2 list into the ts file
如何在 ionic 2 的 ts 文件中使用 AngularFire2 引用? (例如,我参考了我的 Firebase)
this.courses=this.af.database.list('/CoursesList/2017/Software/A/SemA');
我想检查 ts 文件(而不是 HTML 文件)中的迭代,并对该数据执行某些操作。可能吗?
我尝试这样做但出现错误
这是我的代码
this.courses
.subscribe(snapshots => {
snapshots.forEach(snapshot => {
console.log(snapshot.val());
});
是的!有多种方法可以这样做。使用 .push()
、.set()
、.update()
和.delete()
.
最简单的方法是调用 .subscribe()
并遍历实际数组。
af.database.list('/songs').subscribe(songs => {
// songs is the downloaded array
// type could simply be any[] or whatever model you need
this.songs = songs;
// or you can forEach or whatnot
this.songs.forEach(song => console.log(song));
});
更有利的方法是使用 Observable
运算符,例如 .map()
:
af.database.list('/songs')
// Almost like a regular array map, but it emits for each item
// added over time
.map(song => {
song.name = song.name.toUpperCase();
return song;
})
.subscribe(songs => {
// songs is the downloaded array
this.songs = songs;
// All the names will be in caps
this.songs.forEach(song => console.log(song.name));
});
确保你导入了 .map()
运算符,否则它不会工作!
import 'rxjs/add/operator/map`
如何在 ionic 2 的 ts 文件中使用 AngularFire2 引用? (例如,我参考了我的 Firebase)
this.courses=this.af.database.list('/CoursesList/2017/Software/A/SemA');
我想检查 ts 文件(而不是 HTML 文件)中的迭代,并对该数据执行某些操作。可能吗?
我尝试这样做但出现错误
这是我的代码
this.courses
.subscribe(snapshots => {
snapshots.forEach(snapshot => {
console.log(snapshot.val());
});
是的!有多种方法可以这样做。使用 .push()
、.set()
、.update()
和.delete()
.
最简单的方法是调用 .subscribe()
并遍历实际数组。
af.database.list('/songs').subscribe(songs => {
// songs is the downloaded array
// type could simply be any[] or whatever model you need
this.songs = songs;
// or you can forEach or whatnot
this.songs.forEach(song => console.log(song));
});
更有利的方法是使用 Observable
运算符,例如 .map()
:
af.database.list('/songs')
// Almost like a regular array map, but it emits for each item
// added over time
.map(song => {
song.name = song.name.toUpperCase();
return song;
})
.subscribe(songs => {
// songs is the downloaded array
this.songs = songs;
// All the names will be in caps
this.songs.forEach(song => console.log(song.name));
});
确保你导入了 .map()
运算符,否则它不会工作!
import 'rxjs/add/operator/map`