概述我正在学习Angular和JHipster,我正在尝试获取集合中对象的id。
我正在尝试使用trackBy获取id,但我正在获得无限循环
认为这是因为轨道通过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);
}
}
现在你可以看到我评论了函数im里面的服务parte调用trackId
当我这样做时我没有循环
这是我的控制台看完全代码的人:
永远都不要停止
但随着代码评论:
所以我只是想让id来调用服务并获得DOFA分析的元素
diagnosticoFoda.id
所以也许是一个更好的方式trackTy功能我开放的建议。如果这将修复它,但你的trackId函数没有返回任何东西。添加一个回报吧!希望这有助于一些......
我不能发表评论,所以我把它作为答案