首页 > 解决方案 > 按特定 JSON 值过滤以绑定 Angular 5

问题描述

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

json结构

我希望能够对键值执行绑定,即能够执行 array.company(并显示值的内容,例如:“Anonymous 3 Company SA”)。这可能吗?我只设法一次打印整个对象:

<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++;
});

非常感谢!

标签: jsonangularbinding

解决方案


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>

推荐阅读