I am building an app using Angular 4, AngularFire2, and Firebase.
I am running into an issue.
In my app-component.html I have this:
<app-person *ngIf="isPerson" [personId]="personId" [treeUniqueName]="treeUniqueName" [treeId]="treeId"></app-person>
In my app-component.ts I have this:
import { Component } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
isTree = false;
treeId;
treeUniqueName = '';
treeDisplayName = '';
isPerson = false;
personId = '';
isLocation = false;
locationId = '';
isEvent = false;
eventId = '';
isDocument = false;
documentId = '';
isSitemap = false;
data = '';
headers = new Headers();
constructor(
protected db: AngularFireDatabase
) {}
ngOnInit() {
let cleanPath = location.pathname.toLowerCase().replace(/\/+$/, '');
//console.info(cleanPath);
cleanPath = cleanPath || '';
if (cleanPath === '/sitemap') {
this.isSitemap = true;
}
let routeSegments = cleanPath.split("/");
//console.info(routeSegments);
if (routeSegments.length >= 3 && routeSegments[1] == 'family')
{
this.treeUniqueName = routeSegments[2].toLowerCase();
let output = this.db.list('/tree', {
query: {
orderByChild: 'unique',
equalTo: this.treeUniqueName
}
}).subscribe(tree => {
this.treeId = tree[0].$key;
this.treeDisplayName = tree[0].display;
console.log(this.treeId);
})
if (routeSegments.length == 3)
{
this.isTree = true;
}
else if (routeSegments.length == 5 && routeSegments[3].toLowerCase() == 'person')
{
this.isPerson = true;
this.personId = routeSegments[4].toLowerCase();
}
else if (routeSegments.length == 5 && routeSegments[3].toLowerCase() == 'location')
{
this.isLocation = true;
this.locationId = routeSegments[4].toLowerCase();
}
else if (routeSegments.length == 5 && routeSegments[3].toLowerCase() == 'event')
{
this.isEvent = true;
this.eventId = routeSegments[4].toLowerCase();
}
else if (routeSegments.length == 5 && routeSegments[3].toLowerCase() == 'document')
{
this.isDocument = true;
this.documentId = routeSegments[4].toLowerCase();
}
}
}
}
If I inspect the page, I see that the data attributes are being correctly bound:
<app-person ng-reflect-tree-id="964f000f-573b-481b-9e43-18bc77" ng-reflect-tree-unique-name="doe" ng-reflect-person-id="b051cb21-6419-4b6f-85b5-d0891bc2a166"><!--bindings={
"ng-reflect-ng-for-of": ""
}--></app-person>
But then in my person.component.ts I have this:
import { Component, Input, OnInit, OnDestroy } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Title, Meta } from '@angular/platform-browser';
import { EventPipe } from './event.pipe';
@Component({
selector: 'app-person',
templateUrl: './person.component.html'
})
export class PersonComponent implements OnInit, OnDestroy {
@Input() treeId: string;
@Input() treeUniqueName: string;
@Input() personId: string;
subscriptions: Array<any> = new Array<any>();
people: Array<any> = new Array<any>();
constructor(
private title: Title,
private meta: Meta,
protected db: AngularFireDatabase
){}
ngOnInit() {
console.log(this.treeId); // FOR SOME REASON THIS IS EMPTY
this.subscriptions.push(
this.db.list('/person', {
query: {
queryByChild: 'tree',
equalTo: this.treeId,
limitToLast: 200
}
}).subscribe(people => {
this.people = people;
})
);
}
ngOnDestroy() {
this.subscriptions.forEach(s => s.unsubscribe());
}
}
For some reason this.treeId is always empty.
I am guessing this has to do with page lifecycle? If so, how do I get around it?