[英]Loop over nested array of objects
I'm having a hard time trying to loop over an array of nested objects using ng7 我在尝试使用ng7遍历嵌套对象数组时遇到了困难
this is the data I have: 这是我的数据:
data = {
'title1': [
{
active: true,
id: 1
},
{
active: true,
id: 2
},
{
active: true,
id: 3
},
{
active: true,
id: 4
}],
'title2': [
{
active: true,
id: 1
},
{
active: true,
id: 2
},
{
active: true,
id: 3
},
{
active: true,
id: 4
}]
}
I need to print the titles eg 'title1' and the rest of the data should be shown nested, issue is, whenever I go for this approach, everything looks ok: 我需要打印标题,例如“ title1”,其余数据应嵌套显示,问题是,每当我采用这种方法时,一切看起来都很好:
<ul>
<li *ngFor="let item of data| keyvalue">
<p>{{ item.key }}</p>
<p *ngFor="let children of item.value | keyvalue" >
{{ children.value.id}}
</p>
</li>
</ul>
But whenever I switch my layout to an input checkbox like this: 但是每当我将布局切换到这样的输入复选框时:
<ul>
<li *ngFor="let item of data| keyvalue">
<p>{{ item.key }}</p>
<label>
<input type="checkbox" name="events" *ngFor="let children of item.value | keyvalue" />
event item {{ children.value.id}}
</label>
</li>
</ul>
I get the following error on the browser's console, and nothing renders: 我在浏览器的控制台上收到以下错误,但没有任何显示:
ERROR TypeError: Cannot read property 'value' of undefined at Object.eval [as updateRenderer] on 错误TypeError:无法读取Object.eval上undefined的属性'value'[作为updateRenderer]
{{ item.key }}
{{item.key}}
Any idea? 任何想法? I'm sure I'm missing something dumb,
我确定我想念一些愚蠢的东西,
Your children
reference is not in scope when you reference it because the event item {{children.value.id}}
string is not contained in the <input>
element. 引用时,您的
children
引用不在范围内,因为event item {{children.value.id}}
字符串不包含在<input>
元素中。
Define the loop on your <label>
instead of your <input>
element: 在您的
<label>
而不是<input>
元素上定义循环:
<ul>
<li *ngFor="let item of data | keyvalue">
<p>{{ item.key }}</p>
<label *ngFor="let children of item.value | keyvalue">
<input type="checkbox" name="events" />
event item {{children.value.id}}
</label>
</li>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.