dan*_*iel 2 arrays json angular
我想用角度2*ngFor列出以下json的状态和类型.还应呈现父母属性.
var test= {
'192-168-0-16':{
'ac395516-96d0-4533-8e0e-efbc68190e':{
'state': "none",
'typ': "test"
},
'ac395516-96d0-4533-8e0e-efbc68190e':{
'state': "none",
'typ': "dummy"
}
},
'222-21-12-12': {
'ac395516-96d0-4533-8e0e-efbc68190e': {
'state': "none",
'typ': "none"
}
}
Run Code Online (Sandbox Code Playgroud)
角度2:
<div *ngFor='#s of test'> <!-- also other properties should be visible -->
<div *ngFor='#t of s'>
{{t.state}} {{t.typ}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
当然这不起作用.没有很多数据转换是否可能?我不知道如何获取我的json数组.
你需要使用数组ngFor.如果要在此级别使用对象,则应考虑使用自定义管道.
有关详细信息,请参阅此答案:
基于此,我将以这种方式重构您的代码:
<div *ngFor='#s of (test | keyValues)'>
<div *ngFor='#t of (s.value | keyValues)'>
{{t.value.state}} {{t.value.typ}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
使用以下管道:
@Pipe({name: 'keyValues'})
export class KeysPipe implements PipeTransform {
transform(value, args:string[]) : any {
let keys = [];
for (let key in value) {
keys.push({key: key, value: value[key]);
}
return keys;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6113 次 |
| 最近记录: |