[英]Angular - Cannot read property 'employees' of undefined
I'm trying to display some datas with *ngFor angular directive in multi-dimensional array. 我正在尝试使用* ngFor角度指令在多维数组中显示一些数据。 The first ngFor is going well but I got an error on the third ngFor:
第一个ngFor进行得很好,但第三个ngFor出现了错误:
Cannot read property 'employees' of undefined
无法读取未定义的属性“员工”
It means that my second ngFor is producing and undefined employee
variable. 这意味着我的第二个ngFor正在生成和未定义的
employee
变量。 Why is this happening ? 为什么会这样呢?
What am I doing wrong ? 我究竟做错了什么 ?
The "alphas" array is a private property of the class linked to this template, and contains 1 value with the letter 'B' as well as at least one workplace in the "workplaces" array, the workplace itself containing at least one employee in the "employees" array. “ alphas”数组是链接到此模板的类的私有属性,并且包含1个带有字母“ B”的值以及“ workplaces”数组中的至少一个工作场所,该工作场所本身至少包含一名员工。 “员工”数组。 Isn't this should work ?
这不行吗?
Here's the code of my template: 这是我的模板的代码:
<ion-item-group *ngFor="let alpha of alphas">
<ion-item-divider color="light" *ngFor="let workplace of alpha.workplaces">{{alpha.letter}}</ion-item-divider>
<ion-item *ngFor="let employee of workplace.employees">
<ion-avatar item-start>
<img src="assets/imgs/BK.png"> <!-- changer pour {{workplace.imgUrl}} -->
<ion-icon *ngIf="employee.connected" name="md-square" color="orange" style="font-size: 10px; position: absolute;left: 50px;bottom: 22.5px;">
</ion-icon>
</ion-avatar>
<h2>
Zerdiz, 35
<ion-icon name="md-star" color="orange"></ion-icon>
<ion-icon name="md-star" color="orange"></ion-icon>
<ion-icon name="md-star" color="orange"></ion-icon>
<ion-icon name="md-star-half" color="orange"></ion-icon>
<ion-icon name="md-star-outline" color="orange"></ion-icon>
</h2>
<h3>Burger King Nice</h3>
<p>Service</p>
</ion-item>
</ion-item-group>
Thanks to anyone who will take the time to read/answer. 感谢所有花时间阅读/回答的人。
Try using safe navigation operator as follows, 尝试按以下方式使用安全的导航运算符,
<ion-item-divider color="light" *ngFor="let workplace of alpha?.workplaces">{{alpha?.letter}}</ion-item-divider>
<div *ngFor="let employee of workplace?.employees">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.