我正在使用AngularFire 2构建一个Ionic 2应用程序,以便与Firebase进行通信。
以下是我的Firebase数据库:
{
"films" : [
{
"title" : "John Wick",
"summary": "Ex-assassin John Wick takes down the Russian Mafia after being violenty attacked by Russian thugs in his home after the death of his wife",
"date" : "2014",
"duration": "97 minutes",
"rating": "15",
"cover" : "none",
"actors" : {
"keanureeves" : true,
"willemdafoe" : true,
"michaelnyqvist" : true,
"ianmcshane" : true,
"johnleguizamo" : true,
"bridgetmoynahan" : true,
"alfieallen" : true,
"adriannepalicki" : true
},
"genres" : {
"action" : true,
"thriller" : true
},
"images" : {
"image1" : true,
"image2" : true,
"image3" : true,
"image4" : true,
"image5" : true
}
},
{
"title" : "Gladiator",
"summary": "The general who became a slave, the slave who became a gladiator, the gladiator who defied an empire",
"date" : "2000",
"duration": "2hr 29 minutes",
"rating": "15",
"cover" : "none",
"actors" : {
"russellcrowe" : true,
"oliverreed" : true
},
"genres" : {
"action" : true
},
"images" : {
"image6" : true
}
},
{
"title" : "Catch me if you can",
"summary": "Frank Abagnale Jr, master of deception",
"date" : "2002",
"duration": "135 minutes",
"rating": "12",
"cover" : "none",
"actors" : {
"tomhanks" : true,
"leonardodicaprio" : true,
"christopherwalken" : true
},
"genres" : {
"comedy" : true,
"thriller" : true
},
"images" : {
"image7" : true,
"image8" : true
}
},
{
"title" : "Street Kings",
"summary": "Corrupt, murderous cops in Los Angeles",
"date" : "2008",
"duration": "104 minutes",
"rating": "15",
"cover" : "none",
"actors" : {
"forestwhitaker" : true,
"keanureeves" : true,
"chrisevans" : true
},
"genres" : {
"crime" : true,
"action" : true,
"thriller" : true
},
"images" : {
"image9" : true
}
}
],
"images" : {
"image1" : {
"image" : "ref",
"caption": "Keanu Reeves as John Wick"
},
"image2" : {
"image" : "ref",
"caption": "Taking down the Russian mafia"
},
"image3" : {
"image" : "ref",
"caption": "Nightclub scene #1"
},
"image4" : {
"image" : "ref",
"caption": "Nightclub scene #2"
},
"image5" : {
"image" : "ref",
"caption": "Nightclub scene #3"
},
"image6" : {
"image" : "ref",
"caption": "Maximus the Gladiator"
},
"image7" : {
"image" : "ref",
"caption": "Leonardo DiCaprio as Frank Abagnale Jr"
},
"image8" : {
"image" : "ref",
"caption": "Panam-Air hostesses"
},
"image9" : {
"image" : "ref",
"caption": "Keanu reeves as Detective Tom Ludlow"
}
},
"actors" : {
"keanureeves" : {
"name" : "Keanu reeves",
"nationality" : "Canadian"
},
"willemdafoe" : {
"name" : "Willem Dafoe",
"nationality" : "Dutch"
},
"michaelnyqvist" : {
"name" : "Michael Nyqvist",
"nationality" : "Swedish"
},
"ianmcshane" : {
"name" : "Ian McShane",
"nationality" : "English"
},
"johnleguizamo" : {
"name" : "John Leguizamo",
"nationality" : "Colombian-American"
},
"bridgetmoynahan" : {
"name" : "Bridget Moynahan",
"nationality" : "American"
},
"alfieallen" : {
"name" : "Alfie Allen",
"nationality" : "English"
},
"adiannepalicki" : {
"name" : "Adrianne Palicki",
"nationality" : "American"
},
"russellcrowe" : {
"name" : "Russell Crowe",
"nationality" : "New Zealander"
},
"oliverreed" : {
"name" : "Oliver Reed",
"nationality" : "English"
},
"leonardodicaprio" : {
"name" : "Leonardo DiCaprio",
"nationality" : "American"
},
"tomhanks" : {
"name" : "Tom Hanks",
"nationality" : "American"
},
"christopherwalken" : {
"name" : "Christopher Walken",
"nationality" : "American"
},
"martinsheen" : {
"name" : "Martin Sheen",
"nationality" : "American"
},
"forestwhitaker" : {
"name" : "Forest Whitaker",
"nationality" : "American"
},
"chrisevans" : {
"name" : "Chris Evans",
"nationality" : "American"
}
},
"genres" : {
"action" : {
"title": "Action/Adventure"
},
"fantasy" : {
"title": "Fantasy"
},
"horror" : {
"title": "Horror"
},
"comedy" : {
"title": "Comedy"
},
"thriller" : {
"title": "Thriller"
},
"sciencefiction" : {
"title": "Science Fiction"
},
"war" : {
"title": "War"
},
"crime" : {
"title": "Crime"
},
"documentary" : {
"title": "Documentary"
}
}
}
我已将数据非规范化为特定集合,以避免冗余/重复等(根据Firebase的最佳实践建议)。
请忽略images集合中的ref值,因为它们暂时只是占位符:)
这是我的home.ts文件:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { AngularFire, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public movies : FirebaseListObservable<any[]>;
constructor(public navCtrl : NavController,
public angFire : AngularFire)
{
this.movies = angFire.database.list('/movies/films');
}
}
在这里,我使用AngularFire 2作为列表检索电影集合。
电影系列还包含将数据链接到流派,演员和图像集的关键。
我的home.html文件结构如下:
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item *ngFor="let movie of movies | async">
<h2>{{ movie.title }}</h2>
<p>{{ movie.duration }}</p>
<p>{{ movie.date }}</p>
</ion-item>
</ion-list>
</ion-content>
我可以检索电影收藏并在组件视图中显示每部电影而没有问题。
我想要做的是根据电影集合中每条记录的actors节点中的索引从actors集合中检索数据。
因此,使用电影集合中记录的这个节点中的每个键:
"actors" : {
"keanureeves" : true,
"willemdafoe" : true,
"michaelnyqvist" : true,
"ianmcshane" : true,
"johnleguizamo" : true,
"bridgetmoynahan" : true,
"alfieallen" : true,
"adriannepalicki" : true
},
通过提供的密钥从actors集合中检索匹配数据:
"actors" : {
"keanureeves" : {
"name" : "Keanu reeves",
"nationality" : "Canadian"
},
"willemdafoe" : {
"name" : "Willem Dafoe",
"nationality" : "Dutch"
},
"michaelnyqvist" : {
"name" : "Michael Nyqvist",
"nationality" : "Swedish"
},
"ianmcshane" : {
"name" : "Ian McShane",
"nationality" : "English"
},
"johnleguizamo" : {
"name" : "John Leguizamo",
"nationality" : "Colombian-American"
},
"bridgetmoynahan" : {
"name" : "Bridget Moynahan",
"nationality" : "American"
},
"alfieallen" : {
"name" : "Alfie Allen",
"nationality" : "English"
},
"adiannepalicki" : {
"name" : "Adrianne Palicki",
"nationality" : "American"
}
}
这有意义吗?
我正在试图弄清楚如何实现这一目标。
过去几个小时我一直在网上搜索,但我尝试过的一切都没用。
我可以查询单个集合,但实际上,我想制作一个关系查询,该查询使用电影集合中的索引来检索来自演员,流派和图像集合的匹配记录。
有没有办法使用AngularFire 2根据特定密钥从不同的集合中检索数据并在一次调用中返回?