【发布时间】:2019-05-14 12:08:56
【问题描述】:
我正在尝试编写一个应该包含项目的一些对象的数组,但我总是收到一个错误消息,称为:“this.allgmArray is undefined”但实际上不是:
allgmArray: DialogBook[];
[...]
load(){
for(var i = 0; i < this.service.allgmTempArray.length; i++) {
if(this.service.allgmTempArray[i].type == this.service.tempGroup.get('type').value){
var fullName = this.service.tempGroup.get('fullName').value;
console.log(fullName) //works fine
var type = this.service.allgmTempArray[i].type;
console.log(type) //works fine
var device = this.service.allgmTempArray[i].device;
console.log(device) //works fine
var date_from = this.service.tempGroup.get('date_from').value;
console.log(date_from) //works fine
var date_to = this.service.tempGroup.get('date_to').value;
console.log(date_to) //works fine
var date_from_og = this.service.tempGroup.get('date_from_og').value;
console.log(date_from_og) //works fine
var date_to_og = this.service.tempGroup.get('date_to_og').value;
console.log(date_to_og) //works fine
var obj = {
fullName: fullName,
type: type,
device: device,
date_from: date_from,
date_to: date_to,
date_from_og: date_from_og,
date_to_og: date_to_og
}
console.log(obj) //<- Comes out the right obj
this.allgmArray.push(obj) //<- I think heres the problem
}
}
console.log(this.allgmArray) //Error: this.allgmArray is undefined
//And "DialogBook" interface look like:
export interface DialogBook {
fullName: String;
device: String;
type: String;
date_from: String;
date_to: String;
date_from_og: Date;
date_to_og: Date;
}
所以我真的不知道问题是什么,需要一些帮助
【问题讨论】:
-
DialogBook是Object所以这个错误来了。allgmArray: DialogBook[] = [];试试这个 -
在定义时尝试初始化 allgmArray。 allgmArray: DialogBook[]=[];
标签: arrays angular undefined push