I have been trying to learn angular 2 following along with this tutorial [Build an application with Angular 2 and Firebase][1]
and trying to extend on it. But I have hit a snag when trying to nest multiple routes.
App structure:
Goals – (has router-outlet)
> Single Goal with Experiments list – (has router-outlet)
> Single Experiment – (has router-outlet)
> Experiment Notes
Router setup:
export const routerConfig : Route[] = [
{
path: 'goals',
children: [
{
path: ':id', component: SingleGoalComponent,
children: [
{
path: 'experiments',
children: [
{ path: ':id', component: ExperimentDetailsComponent,
children: [
{ path: '', redirectTo: 'notes', pathMatch: 'full' },
{ path: 'notes', component: ExperimentNotesComponent }
]
},
{ path: 'new', component: NewExperimentComponent },
{ path: '' }
]
},
{ path: '', redirectTo: 'experiments', pathMatch: 'full' }
]
},
{ path: '', component: GoalsComponent }
]
},
{ path: 'notes', component: NotesComponent },
{ path: '', redirectTo: 'goals', pathMatch: 'full' },
{ path: '**', redirectTo: 'goals', pathMatch: 'full' }
];
The Problem
If I click on Experiment 1 in the Experiment List I got to goals/1/experiments/1/notes
the url is correct and I see the correct Experiment 1's Notes.
If I then click on Experiment 2 in the Experiment List goals/1/experiments/2/notes
the url is correct the experiment details are correct but the notes are still Experiment 1's Notes.
If I then refresh the browser, Experiment 2 to loads and the notes are now Experiments 2's Notes which is correct.
This is how I get the experimentId
for retrieving the notes
experiment-notes.component.ts
experimentId: string;
goalId: string;
constructor(
private router: Router,
private route: ActivatedRoute,
private experimentsService: ExperimentsService,
private _location: Location) { }
ngOnInit() {
Observable.combineLatest(this.route.parent.params, this.route.parent.parent.params)
.forEach((params: Params[]) => {
this.experimentId = params[0]['id'];
this.goalId = params[1]['id'];
});
console.log('Experiment ID: ' + this.experimentId + '| Goal Id: ' + this.goalId);
this.notes$ = this.experimentsService.findAllNotesForExperiment(this.experimentId);
I'm sure it's an obvious mistake I'm making but for the life of me I can't see where I am going wrong with this.