本文介绍了Angular2访问嵌套JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Typescript中Angular 2的新手.我想使用NgFor访问JSON的D和G.有访问元素的方法吗?
i´m new to Angular 2 in Typescript.I want to access D and G of my JSON with NgFor. Is there a way to access the elements?
[
{
"A":"B",
"C":{
"D": ["E","F"],
"G": ["H"]
}
}
]
我还创建了一个柱塞:柱塞
I also createt a Plunker: Plunker
推荐答案
ngFor无法即时访问对象的键.您必须自己处理.
ngFor can't iterate over an object's keys out of the box. You must handle that yourself.
管道工作良好.示例:更新了Plunkr
Pipes work well. Example: Updated Plunkr
@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
transform(value: any, args?: any[]): any[] {
let keys = Object.keys(value),
data = [];
keys.forEach(key => {
data.push(value[key]);
});
return data;
}
}
这篇关于Angular2访问嵌套JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!