按特定JSON值过滤以绑定Angular 5

时间:2018-06-04 07:33:28

标签: json angular binding

我有一个这方面的JSON对象:

json structure

我希望能够对键值执行绑定,也就是说,能够执行array.company(并显示值的内容,例如:" Anonymous 3 Company S.A& #34)。这可能吗?我只是设法立刻打印整个对象:

<div *ngFor="let sa of serverAttributes">
    {{ sa.key}}
    {{sa.value}}
</div>

这是我的.ts文件:

this.subscriptions = this.cbank.getAssetServerAttributes(this.localCustomer, data[indexx]).subscribe(vres => {
    this.serverAttributes.push(vres[indexx]);
    indexx++;
});

非常感谢!

1 个答案:

答案 0 :(得分:0)

create a custom pipe to return the list of key and value You could also return an entry containing both key and value:

@Pipe({name: 'keys'})
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;
  }
}
and use it like that:

<span *ngFor="let entry of content | keys">           
  Key: {{entry.key}}, value: {{entry.value}}
</span>