Обзор Я изучаю Angular и JHipster и пытаюсь получить идентификатор объекта в коллекции.
Я пытаюсь получить идентификатор с помощью trackByно я получаю бесконечный цикл
думаю, что это происходит, потому что отслеживание по ejecute на каждой итерации, но это странно, потому что функция генерирует другую коллекцию объектов
Это HTML-компонент
<ngb-panel *ngFor="let diagnosticoFoda of diagnosticoFodas;trackBy: trackId " >
И это компонент TS
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);
}
}
Теперь, как вы можете видеть, я прокомментировал parte службы внутри функции, которую я вызываю наtrackId
и когда я делаю это, у меня нет петли
Вот кто моя консоль смотрит с полным кодом:
и никогда не останавливаться.
но с прокомментированным кодом:
, поэтому я просто пытаюсь получить идентификатор для вызова службы и получить элементыанализ DOFA
- Примечания
- Я действительно новичок в Angular, TypeScript и Jhipster.
- Пожалуйста, если я пропустил что-то важное, дайте мне знать в комментарии, и я добавлю к вопросу.
- Я просто пытаюсь получить
diagnosticoFoda.id
, так что, возможно, это лучший способ, чтобы функция trackBy была открыта для предложений.