我想使用插值来访问数组索引处的特定项,但这给了我这个错误:“ TypeError:无法设置未定义的属性'0'。”
当数组未定义但我已经在构造函数中定义它时会发生这种情况。
[
{
"studyID": "12345",
"date": "Thu Jul 18 2019 16:05:58 GMT-0700",
"sex": "f",
"dateOfBirth": "1987-01-02",
"minHz": "25.0",
"maxHz": "55.0",
"session": [
{
"incr": "33.2",
"decr": "34.7"
},
{
"incr": "34.0",
"decr": "30.8"
}
],
"average": "31.5",
"variance": "2.3"
},
,但已被字符串化并解析为一个对象。
for (var i = 0; i < this.parsedData.length; i++) {
this.dateObjects[i] = new Date(this.parsedData[i].date);
}
<div class="table" *ngFor="let data of parsedData; index as i">
Date: {{ this.dateObjects[i] | date:'fullDate'}}
</div>
我希望它可以打印出dateObjects数组中的日期,但是会导致错误:TypeError:无法设置未定义的属性“ 0”。
答案 0 :(得分:0)
因为您已经遍历parsedData并将它们存储在数组(dataObjects)中,所以我认为应该使用HTML遍历dataObjects而不是parsedData
<div class="table" *ngFor="let data of this.dataObjects; index as i">
Date: {{ data | date:'fullDate'}} // here data will be your each array element
</div>