在 angular 2 中在 Ngfor 上迭代一个 json 对象
iteration a json object on Ngfor in angular 2
我在 Ngfor 中迭代 json 对象时遇到问题,有我的模板:
模板:
<h1>Hey</h1>
<div>{{ people| json}}</div>
<h1>***************************</h1>
<ul>
<li *ngFor="#person of people">
{{
person.label
}}
</li>
</ul>
people 是我要迭代的 json 对象,我得到的是 (people | json) 的结果,但没有得到列表,这是屏幕截图:
最后,这是 json 文件的一部分:
{
"actionList": {
"count": 35,
"list": [
{
"Action": {
"label": "A1",
"HTTPMethod": "POST",
"actionType": "indexation",
"status": "active",
"description": "Ajout d'une transcription dans le lac de données",
"resourcePattern": "transcriptions/",
"parameters": [
{
"Parameter": {
"label": "",
"description": "Flux JSON à indexer",
"identifier": "2",
"parameterType": "body",
"dataType": "json",
"requestType": "Action",
"processParameter": {
"label": "",
"description": "Flux JSON à indexer",
"identifier": "4",
"parameterType": "body",
"dataType": "json",
"requestType": "Process"
}
}
},
请随时帮助我
您的 people
对象不是数组,因此您可以开箱即用地对其进行迭代。
有两个选项:
您想迭代一个子 属性。例如:
<ul>
<li *ngFor="#person of people?.actionList?.list">
{{
person.label
}}
</li>
</ul>
您想迭代对象的键。在这种情况下,您需要实现自定义管道:
@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
transform(value, args:string[]) : any {
if (!value) {
return value;
}
let keys = [];
for (let key in value) {
keys.push({key: key, value: value[key]});
}
return keys;
}
}
并这样使用它:
<ul>
<li *ngFor="#person of people | keys">
{{
person.value.xx
}}
</li>
</ul>
有关详细信息,请参阅此答案:
我在 Ngfor 中迭代 json 对象时遇到问题,有我的模板:
模板:
<h1>Hey</h1>
<div>{{ people| json}}</div>
<h1>***************************</h1>
<ul>
<li *ngFor="#person of people">
{{
person.label
}}
</li>
</ul>
people 是我要迭代的 json 对象,我得到的是 (people | json) 的结果,但没有得到列表,这是屏幕截图:
最后,这是 json 文件的一部分:
{
"actionList": {
"count": 35,
"list": [
{
"Action": {
"label": "A1",
"HTTPMethod": "POST",
"actionType": "indexation",
"status": "active",
"description": "Ajout d'une transcription dans le lac de données",
"resourcePattern": "transcriptions/",
"parameters": [
{
"Parameter": {
"label": "",
"description": "Flux JSON à indexer",
"identifier": "2",
"parameterType": "body",
"dataType": "json",
"requestType": "Action",
"processParameter": {
"label": "",
"description": "Flux JSON à indexer",
"identifier": "4",
"parameterType": "body",
"dataType": "json",
"requestType": "Process"
}
}
},
请随时帮助我
您的 people
对象不是数组,因此您可以开箱即用地对其进行迭代。
有两个选项:
您想迭代一个子 属性。例如:
<ul> <li *ngFor="#person of people?.actionList?.list"> {{ person.label }} </li> </ul>
您想迭代对象的键。在这种情况下,您需要实现自定义管道:
@Pipe({name: 'keys'}) export class KeysPipe implements PipeTransform { transform(value, args:string[]) : any { if (!value) { return value; } let keys = []; for (let key in value) { keys.push({key: key, value: value[key]}); } return keys; } }
并这样使用它:
<ul> <li *ngFor="#person of people | keys"> {{ person.value.xx }} </li> </ul>
有关详细信息,请参阅此答案: