Overview I'm learning Angular and JHipster, and I'm trying to get the id of an object in a collection.
I'm trying to get the id using trackBy but im getting a infinity loop
think this is happen because the track by ejecute on each iteration but its strange because the function is generation a different object collection
This is the html component
<ngb-panel *ngFor="let diagnosticoFoda of diagnosticoFodas;trackBy: trackId " >
And this is the TS component
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpErrorResponse, HttpHeaders, HttpResponse } from '@angular/common/http';
import { JhiEventManager, JhiParseLinks, JhiAlertService } from 'ng-jhipster';
import { DiagnosticoFodaService } from 'app/entities/diagnostico-foda';
import { IPlanEstrategico } from 'app/shared/model/plan-estrategico.model';
import { IDiagnosticoFoda } from 'app/shared/model/diagnostico-foda.model';
import {IElementosDiagnosticoFoda} from 'app/shared/model/elementos-diagnostico-foda.model';
import { ElementosDiagnosticoFodaService } from 'app/entities/elementos-diagnostico-foda';
@Component({
selector: 'sigem-plan-estrategico-detail',
templateUrl: './plan-estrategico-detail.component.html'
})
export class PlanEstrategicoDetailComponent implements OnInit {
planEstrategico: IPlanEstrategico;
diagnosticoFodas: IDiagnosticoFoda[];
elementosDiagnosticoFodas : IElementosDiagnosticoFoda[];
elementosFodas: IDiagnosticoFoda[];
idPlan : number;
constructor(
private jhiAlertService: JhiAlertService,
private activatedRoute: ActivatedRoute,
private diagnosticoFodaService: DiagnosticoFodaService,
private elementosDiagnosticoFodaService : ElementosDiagnosticoFodaService) {}
ngOnInit() {
this.activatedRoute.data.subscribe(({ planEstrategico }) => {
this.planEstrategico = planEstrategico;
this.idPlan = planEstrategico.id;
this.cargarAnaliziFoda(this.idPlan);
});
}
previousState() {
window.history.back();
}
private onError(errorMessage: string) {
this.jhiAlertService.error(errorMessage, null, null);
}
cargarAnaliziFoda(id){
this.diagnosticoFodaService.findByPlan(id).subscribe(
(res: HttpResponse<IDiagnosticoFoda[]>) => {
this.diagnosticoFodas = res.body;
},
(res: HttpErrorResponse) => this.onError(res.message)
);
}
cargarElementosFoda(id_foda){
console.log('el id de este diagnostico foda es' + id_foda);
/*this.elementosDiagnosticoFodaService.findByFODA(id_foda).subscribe(
(res: HttpResponse<IElementosDiagnosticoFoda[]>) => {
this.elementosDiagnosticoFodas = res.body;
console.log(this.elementosDiagnosticoFodas);
},
(res: HttpErrorResponse) => this.onError(res.message)
);*/
}
trackId = (index: number, item: IDiagnosticoFoda) => {
this.cargarElementosFoda(item.id);
}
}
now like you can see I commented the service parte inside the function im calling on the trackId
and when I do that I don't have the loop
This is who my console look with the full code:
and never stop.
but with the code commented:
so i just trying to get the id to call the service and get the elements of the DOFA analysis
- Notes
- I'm really new on Angular, TypeScript, and Jhipster.
- Please if I missed something important, let me know on the comment and I will added to the question.
- Im just trying to get the
diagnosticoFoda.id
so maybe is a better way that trackBy function im open to suggestions.