Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
At click route to details of given object

I have two components: recipe and recipe-detail. Recipe is displaying list of recipes from my database and recipe-detail should display details of given recipe. What I want to achieve is to whenever someone click on recipe name I want to route him to recipe-detail component. This is my recipe html:

<section class="columns">
    <div class="column" *ngFor="let recipe of RecipeList">
        <h2><a routerLink="recipes/{{recipe.Id}}" routerLinkActive="true">{{recipe.name}}</a></h2>
        <p>{{recipe.recipeBody}}</p>
    </div>
</section>  

My routing file:

{path:'recipes', component: RecipeComponent},
{path: 'recipes/:id', component: RecipeDetailsComponent}

And my recipe-detail ts:

export class RecipeDetailsComponent implements OnInit {
  @Input() recipe! : any;
  constructor(private route: ActivatedRoute,
    private sharedService: SharedService) { }

  ngOnInit(): void {
    this.getRecipe();
  }
  
  getRecipe() : void{
    const id = Number(this.route.snapshot.paramMap.get('id'));
    this.sharedService.getRecipeById(id).subscribe(recipe => this.recipe == recipe);
  }
}

Simple html for test:

<h2>Name : {{recipe.name}}</h2>
<h2>RecipeBody: {{recipe.recipeBody}}</h2>
<h2>IntendedUse: {{recipe.IntendedUse}}</h2>
<h2>CoffeeId: {{recipe.coffeeId}}</h2>

Results: When I click on recipe name (recipe.component) it redirects me to: http://localhost:4200/recipes/recipes when it sould be for example http://localhost:4200/recipes/1

And in the console i get: TypeError: Cannot read properties of undefined (reading 'name')

Edit: How i fetch it:

getRecipes() : Observable<any[]> {
    return this.http.get<any>(this.ApiUrl + '/Recipes')
  }
  getRecipeById(val:any){
    return this.http.get<any>(this.ApiUrl + '/Recipes/', val);
  }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I would recommend doing it as below by subscribing

HTML

<h2><a [routerLink]="['/recipes', recipe.Id]" routerLinkActive="true">{{recipe.name}}</a></h2>


import { ActivatedRoute } from '@angular/router';

constructor(private route: ActivatedRoute,private sharedService: SharedService) {

ngOnInit() {
 this.route.params
      .subscribe(
        (params: Params) => {
         this.sharedService.getRecipeById(params['id']).subscribe(recipe => {
            this.recipe = recipe;
          })
        }
      );
    }
}

or even make it better with switchMap

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to use = to assign the values but not ==.

From:

this.sharedService.getRecipeById(id).subscribe(recipe => this.recipe == recipe);

To:

this.sharedService.getRecipeById(id).subscribe((recipe:any) => this.recipe = recipe);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda