【发布时间】:2019-10-09 21:42:59
【问题描述】:
我使用请求 API 的结果创建多个数组。这是我按下按钮时调用的组件的方法。
ngOnInit() {
// GetListeAnomalies(start_date : string, end_date : string) -> return an array
this.servicePS.getListeAnomalies("2019-10-08", "2019-10-08").then(
(anomalies : any) => {
for (let anomalie of anomalies){
switch (anomalie.type_anomalie) {
case "erreur_deltaT_recurrent": {
this.tab_delta_T_recurrent.push(anomalie);
break;
}
case "erreur_absence_emission": {
this.tab_absence_emission.push(anomalie);
break;
}
case "erreur_delta_T_trop_grand":{
this.tab_deltaT_trop_grand.push(anomalie);
break;
}
case "erreur_capteur_batterie_faible":{
this.tab_capteur_batterie_faible.push(anomalie);
break;
}
case "erreur_device_a_remplacer_grande_derive":{
this.tab_device_a_remplacer_grande_derive.push(anomalie);
break;
}
default: {
this.tab_other.push(anomalie)
}
}
}
}
)
}
然后,我想在名为“tableau-anomalies”的组件中传递这些数组。所以我用这条线:
<app-tableau-anomalies [tabValeurs]="tab_delta_T_recurrent"></app-tableau-anomalies>
在我的组件中,我尝试使用 Input 值,但该值未正确初始化。看看我的组件代码(和我的 cmets)
export class TableauAnomaliesComponent implements OnInit, OnChanges {
@Input() tabValeurs: any;
constructor() {
}
ngOnInit() {
console.log(this.tabValeurs.length); //Return 0
console.log(this.tabValeurs); //Return CORRECTLY my array with my data.
for (let anomalie in this.tabValeurs){
console.log(anomalie); //This iteration is not called --> no return, not even 'undefined' is written in the console.
}
}
}
编辑:对不起,我忘了写我的这部分代码......我也试过“onChanges”。
ngOnChanges(changes: SimpleChanges) {
if (changes.tabValeurs){
console.log(this.tabValeurs.length); //Return 0
for (let anomalie of this.tabValeurs){
console.log(anomalie);//This iteration is not called --> no return, not even 'undefined' is written in the console.
}
}
}
我认为有类似 Promise 或 Subscribe 的解决方案吗?但我不知道如何在我的情况下使用它...... 那么你能帮帮我吗?您有任何解决方案的想法吗?
提前谢谢:)
【问题讨论】:
-
试试用 map 代替 for ?
-
我不知道怎么用..你能解释一下吗?
-
我在我的帖子里做过 :)
标签: angular typescript promise