英文:
*ngFor keeps accessing 2 seperate arrays with the same name concurrently. I only want to Iterate through one
问题
<mat-tab *ngFor="let data of allData" label="{{data.event.useful[0].name}}">
<!-- The part below is fixed -->
<div class="content">
<mat-list role="list">
<mat-list-item role="listitem">{{data.section[0].street_group[0].name}}</mat-list-item>
</mat-list>
</div>
</mat-tab>
英文:
I am trying to Iterate through an Array. The thing is the data is nested and in an object there are 2 arrays with the same name. I would like to Iterate through only one Array. Angular Iterates through both the Arrays concurrently.
I am trying to access and print the name from the first instance of street_group array only. Angular is print the name from both instances of street_group array concurrently.
Here is the Data
{
"generated_at": "-",
"schema": "-",
"event": {
"id": "-",
"scheduled": "-",
"value": false,
"timing": {
"type": "-",
},
"season": {
"id": "-",
"name": "-",
"start_date": "-",
"end_date": "-",
"year": "-",
},
"cycle": {
"id": "",
"name": "-",
"car": {
"id": "-",
"name": "-"
},
"category": {
"id": "-",
"name": "-"
},
"type": "-",
"value": "-"
},
"useful": [{
"id": "-",
"name": "-",
"value": "-",
"value2": "-",
"value3": "-",
"value4": "-"
}, {
"id": "-",
"name": "-",
"value1": "-",
"value2": "-",
"value3": "-",
"value4": "-"
}],
"venue": {
"id": "-",
"name": "-",
"city_name": "-",
"country_name": "-",
"map_coordinates": "--",
"country_code": "-",
"values": [{
"name": "-"
}, {
"name": "-"
}]
}
},
"section": [{
"locale": "-",
"value": {
"id": "-",
"name": "-",
"country_code": "-"
},
"street_group": [{
"id": "-",
"name": "-",
"type": "-",
}, {
"id": "-",
"name": "-",
"type": "-",
}, {
"id": "-",
"name": "-",
"type": "-",
}
]
}, {
"locale": "-",
"value": {
"id": "-",
"name": "-",
"country_code": "-"
},
"street_group": [{
"id": "-",
"name": "-",
"type": "-",
}, {
"id": "-",
"name": "-",
"type": "-",
}, {
"id": "-",
"name": "-",
"type": "-",
}
]
}]
}
Here is My Component
export class LiveComponent implements OnInit {
allData: {
generated_at: string;
section: {
street_group: { name: string }[];
locale: string }[];
event: {
id: string;
useful: { name: string }[];
};
}[] | null = null;
constructor(private http: HttpClient ) {
}
ngOnInit(){
this.http.get('http://api.redacted_for_privacy').subscribe(data => {this.allData = [data as any];});
}
}
This is my HTML
<mat-tab-group>
<mat-tab *ngFor="let data of allData" label="{{data.event.useful[0].name}}">
>! The part below is broken
<div class="content">
<mat-list role="list" *ngFor="let section of data.section">
<mat-list-item role="listitem" *ngFor="let street_group of section.street_group | slice:0:2">{{street_group.name}}</mat-list-item>
</mat-list>
</div>
</mat-tab>
</mat-tab-group>
I have tried adding an Index number to the Section Array since it is the containing array but that breaks the code.
<mat-list role="list" *ngFor="let section of data.section[0]">
Where of gives this error
Type '{ street_group: { name: string; }[]; }' is not assignable to type 'NgIterable<any> | null | undefined'.ngtsc(2322)
I was expecting only the first Array to render.
答案1
得分: 1
如果我理解你的问题正确,你不需要二维循环(double *ngFor),而是在第零个元素上进行单一循环。
要打印出从部分数组的第一个元素开始的所有名称:
- 删除
mat-list
上的外部 *ngFor 指令
只循环遍历 mat-list-item
内部部分的第零个元素
你的HTML可能如下所示:
<mat-list role="list">
<mat-list-item
role="listitem"
*ngFor="let street_group of data.section[0].street_group"
>{{ street_grp.name }}</mat-list-item
>
</mat-list>
请注意 *ngFor="let street_grp of data.section[0].street_group"
,只迭代 data.section[0]
中的元素;
这里还应该提到的是,有时将嵌套数据展平可能会很有用。这简化了对数据的访问和操作。
英文:
If I understand your question correctly, you don't need a two-dimensional loop (double *ngFor), but a single one on the 0th element.
To print all the names from the first element of section array:
- Remove the outer *ngFor directive on
mat-list
Only loop through the 0th element of the section inside mat-list-item
Your HTML could look like:
<mat-list role="list">
<mat-list-item
role="listitem"
*ngFor="let street_group of data.section[0].street_group"
>{{ street_grp.name }}</mat-list-item
>
</mat-list>
notice *ngFor="let street_grp of data.section[0].street_group"
to iterate only the elements in data.section[0]
;
It should also be mentioned here that it can sometimes be useful to flatten nested data. This simplifies access and manipulation of the data.
答案2
得分: -1
没有适用于您在Angular中的目的的管道。我建议进行一个映射,将所有部分中的street_groups连接起来。
// 将在一个数组中输出所有street_groups
data.section.map((section) => section.street_group).flat()
英文:
There is no pipe for your purpose available in Angular. I suggest doing a mapping that will concatenate street_groups in all sections.
// Will output all street_groups in one array
data.section.map((section) => section.street_group).flat()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论