【发布时间】:2019-03-01 12:19:41
【问题描述】:
概述
我正在学习 Angular 和 JHipster,我正在尝试获取集合中对象的 id。
我正在尝试使用 trackBy 获取 id,但出现此错误:
[Error] ERROR – TypeError: this.cargarElementosFoda is not a function. (In 'this.cargarElementosFoda(item.id)', 'this.cargarElementosFoda' is undefined)
TypeError: this.cargarElementosFoda is not a function. (In 'this.cargarElementosFoda(item.id)', 'this.cargarElementosFoda' is undefined)trackIdcheckdiffngDoCheckcheckAndUpdateDirectiveInlinedebugCheckAndUpdateNodedebugCheckDirectivesFn(función anónima)checkAndUpdateViewcallViewActionexecEmbeddedViewsActioncheckAndUpdateViewcallViewActionexecComponentViewsActioncheckAndUpdateViewcallViewActionexecEmbeddedViewsActioncheckAndUpdateViewcallViewActionexecComponentViewsActioncheckAndUpdateViewcallWithDebugContextdetectChangesforEachtick(función anónima)onInvokerunnext(función anónima)__tryOrUnsubnext_nextnextnextemitcheckStableonLeaveonInvokeTaskrunTaskinvokeTaskinvokeTaskglobalZoneAwareCallback
error
View_PlanEstrategicoDetailComponent_1 (PlanEstrategicoDetailComponent.ngfactory.js:337)
logError (core.js:12446)
(función anónima)
handleError (core.js:1922)
run (zone.js:137)
tick (core.js:5374)
(función anónima) (core.js:5210:110)
onInvoke (core.js:4343)
run (zone.js:137)
next (core.js:5210:85)
(función anónima) (core.js:3993)
__tryOrUnsub (Subscriber.js:262)
next (Subscriber.js:200)
_next (Subscriber.js:138)
next (Subscriber.js:102)
next (Subject.js:64)
emit (core.js:3985)
checkStable (core.js:4312)
onLeave (core.js:4379)
onInvokeTask (core.js:4337)
runTask (zone.js:187)
invokeTask (zone.js:495)
invokeTask (zone.js:1539)
globalZoneAwareCallback (zone.js:1576)
我不知道为什么会这样,因为我的所有其他功能都运行良好。
这是 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){
/*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) {
console.log('el id de este diagnostico foda es' + item.id);
this.cargarElementosFoda(item.id);
}
}
和 HTML 组件:
这是我通过 id 调用 track 的 html 部分
<ngb-panel *ngFor="let diagnosticoFoda of diagnosticoFodas;trackBy: trackId">
<ng-template ngbPanelTitle>
<span > Diagnostico FODA {{diagnosticoFoda.nombre}}
</span>
</ng-template>
备注
- 我对 Angular、TypeScript 和 Jhipster 非常陌生。
- 如果我遗漏了重要内容,请在评论中告诉我,我会添加到问题中。
- 我只是想得到
diagnosticoFoda.id所以也许更好 trackBy 函数的方式。
【问题讨论】:
-
你能显示代码所在的完整文件吗?
-
@AGhanima 抱歉,我用完整代码更新了任务。
-
我们需要知道这发生在哪里。我假设您的模板中的某些内容是直接调用 trackId(调用 cargarElementosFoda)或 cargarElementosFoda。但是您发布的代码中没有任何内容实际上会导致 cargarElementosFoda 被调用。
-
@NightCabbage 是的,在 html 模板中,我有一个 Collapse 和不同的 ngb-panels 用于一些许多数据,但这里重要的是 FODA 的一个,所以让我将其添加到问题中。跨度>
标签: angular typescript