TrackBy功能上的无限循环

问题描述 投票:0回答:1

概述我正在学习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当我这样做时我没有循环

这是我的控制台看完全代码的人:

enter image description here

永远都不要停止

但随着代码评论:

enter image description here

所以我只是想让id来调用服务并获得DOFA分析的元素

  • 笔记
  • 我是Angular,TypeScript和Jhipster的新手。
  • 如果我错过了重要的内容,请在评论中告诉我,我将添加到问题中。
  • 我只是想获得diagnosticoFoda.id所以也许是一个更好的方式trackTy功能我开放的建议。
angular typescript jhipster
1个回答
1
投票

如果这将修复它,但你的trackId函数没有返回任何东西。添加一个回报吧!希望这有助于一些......

我不能发表评论,所以我把它作为答案

© www.soinside.com 2019 - 2024. All rights reserved.