Angular 尝试以角度显示向量值时出错

Angular 尝试以角度显示向量值时出错,angular,object,Angular,Object,我有一个由我定义的数据类型(类)的向量。我的错误是,当我尝试从位置1开始打印时,如果我仅尝试打印位置0(如果它正确显示),则会显示以下内容:“\u this.infurniture[1]未定义”。我打印了不动产向量的长度,很明显,如果变量“real”被插入,因为它显示了正确的长度,但我不知道会发生什么。我留下下面的代码: 从'@angular/core'导入{Component,OnInit}; 从“../servicios/hostlist.service”导入{HostlistServic

我有一个由我定义的数据类型(类)的向量。我的错误是,当我尝试从位置1开始打印时,如果我仅尝试打印位置0(如果它正确显示),则会显示以下内容:“\u this.infurniture[1]未定义”。我打印了不动产向量的长度,很明显,如果变量“real”被插入,因为它显示了正确的长度,但我不知道会发生什么。我留下下面的代码:

从'@angular/core'导入{Component,OnInit};
从“../servicios/hostlist.service”导入{HostlistService};
从“jQuery”导入{$,jQuery};
从“../modelos/Inmueble”导入{Inmueble};
@组成部分({
选择器:“应用程序滑块”,
templateUrl:'./slider.component.html',
样式URL:['./slider.component.css']
})
导出类SliderComponent实现OnInit{
Inmueble:Inmueble[]=[];
i=1;
url:string=“d782a4ae-733f-b7c4-ed11-5ba553455e04\u fot01\u c”;
构造函数(私有hostlistService:hostlistService){}
恩戈尼尼特(){
this.cargarJson();
}
前额肌{
this.i=this.i-1;
}
siguienteInmueble(){
this.i=this.i+1;
}
//佩蒂西翁酒店
cargarJson(){
变量foto1,foto2:字符串;
foto1=“fot0”,foto2=“fot”;
var pos:编号;
此.hostlistService.getInmuebles().subscribe(
结果=>{
如果(result.success==true){
for(结果数据的常量项){
pos=0;
常量inmueble=新inmueble();
inmueble.nombre=item.nomb_prod_c;
inmueble.id=item.id;
inmueble.estacionamiento=item.estan_c;
inmueble.baño=项目.banof_c;
console.log(inmueble.nombre);
(item.fot01_c!=”)?inmueble.fotos[0]=“true”:inmueble.fotos[0]=“false”;pos++;
这个.inmuebles.push(inmueble);
console.log(“--->”+this.inmuebles[0].fotos[0]);
console.log(“--->”+this.inmuebles[1].fotos[0]);
}
}
},
错误=>{
console.log(错误);
}
);
}

}
您尚未初始化inmeuble对象的fotos数组。请执行以下操作-

const inmueble = new Inmueble();
inmueble.fotos = [];
inmueble.nombre = item.nomb_prod_c;
inmueble.id = item.id;
inmueble.estacionamiento = item.estan_c;
inmueble.baño = item.banof_c;
console.log(inmueble.nombre);
(item.fot01_c != "") ? inmueble.fotos.push(true): inmueble.fotos.push(false); pos++;

您正在尝试在for循环中记录this.inmuebles[1],并且您也在第一个循环中记录,此时您的inmuebles数组仅包含1个元素。将日志记录(console.log)从for循环中移出

我已经更新了您的cargarJson函数,删除了日志记录:

cargarJson(){
    var foto1,foto2: string;
    foto1 = "fot0", foto2 = "fot";
    var pos: number;
    this.hostlistService.getInmuebles().subscribe(
        result => {
            if(result.success === true){
                for(const item of result.data) {
                    pos = 0;
                    const inmueble = new Inmueble();
                    inmueble.nombre = item.nomb_prod_c;
                    inmueble.id = item.id;
                    inmueble.estacionamiento = item.estan_c;
                    inmueble.baño = item.banof_c;
                    console.log(inmueble.nombre);
                    (item.fot01_c != "") ? inmueble.fotos[0]="true": inmueble.fotos[0]="false"; pos++;

                    this.inmuebles.push(inmueble);
                    }
                 console.log("----->"+this.inmuebles[0].fotos[0]);
                 console.log("----->"+this.inmuebles[1].fotos[0]);
            }

        },
        error => {
            console.log(error);
        }
    );
  }

inmuebles
数组仅包含1个元素时,您正试图在for循环中记录
this.inmuebles[1]
,并且您也在第一个循环中这样做。将日志记录(console.log)从for循环中移出。