Currently, I am in the process of learning Ionic and Angular while attempting to implement routing where information is passed through the path like this:
path: ':recipeId',
My goal is to have a list of items with IDs so that when I click on an item, I can navigate to a detailed page for that specific item.
I have successfully displayed the page with the list of items (let's say it's at /recipes), but upon clicking on an item to go to the detail page (/recipes/id), I encounter the following error:
ERROR TypeError: "_co.loadedRecipe is undefined" View_RecipeDetailPage_0 RecipeDetailPage.html:11 Angular 24 RxJS 5 Angular 11 RecipeDetailPage.html:11:4 View_RecipeDetailPage_0 RecipeDetailPage.html:11 Angular 16 RxJS 5 Angular 11
This points towards the module's (or component's? still learning Angular) HTML code:
<ion-header>
<ion-toolbar>
<ion-title> {{ loadedRecipe.title }} </ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-grid>
<ion-row>
<ion-col>
<ion-img [src]="loadedRecipe.imageUrl"></ion-img>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<h1> {{ loadedRecipe.title }}</h1>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<ion-list>
<ion-item *ngFor="let ig of loadedRecipe.ingredients">
{{ ig }}
</ion-item>
</ion-list>
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
The issue seems to be that loadedRecipe is currently undefined.
Here is the controller TypeScript code:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { RecipesService } from '../recipes.service';
import { Recipe } from '../recipe.model';
@Component({
selector: 'app-recipe-detail',
templateUrl: './recipe-detail.page.html',
styleUrls: ['./recipe-detail.page.scss'],
})
export class RecipeDetailPage implements OnInit {
loadedRecipe: Recipe;
constructor(
private activatedRoute: ActivatedRoute,
private recipesService: RecipesService
) {}
ngOnInit() {
console.log(this.activatedRoute.paramMap);
this.activatedRoute.paramMap.subscribe(paramMap => {
if (!paramMap.has('recipeId')) {
// redirect because we don't have the necessary ID
return;
}
console.log(paramMap);
const recipeId = paramMap.get('recipeId');
this.loadedRecipe = this.recipesService.getRecipe(recipeId);
});
}
}
App-routing module:
import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{ path: '', redirectTo: 'recipes', pathMatch: 'full' },
{
path: 'recipes',
children: [
{
path: '',
loadChildren: './recipes/recipes.module#RecipesPageModule'
},
{
path: ':recipeId',
loadChildren: './recipes/recipe-detail/recipe-detail.module#RecipeDetailPageModule'
}
],
}
];
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule { }
List controller recipes.page.ts:
import { Component, OnInit } from '@angular/core';
import { RecipesService } from './recipes.service';
import { Recipe } from './recipe.model';
@Component({
selector: 'app-recipes',
templateUrl: './recipes.page.html',
styleUrls: ['./recipes.page.scss'],
})
export class RecipesPage implements OnInit {
recipes: Recipe[];
constructor(private recipesService: RecipesService) { }
ngOnInit() {
this.recipes = this.recipesService.getAllRecipes();
}
}
I suspect there might be an issue with the activated route, but as I am new to debugging web applications, any guidance or tips would be greatly appreciated.