【问题标题】:Filter by specific JSON value to binding Angular 5按特定 JSON 值过滤以绑定 Angular 5
【发布时间】:2018-11-13 11:40:13
【问题描述】:

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

json structure

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

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

非常感谢!

【问题讨论】:

标签: json angular binding


【解决方案1】:
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>

【讨论】:

  • 需要在 app.module.ts 文件或根模块中导入 KeysPipe。
  • 您可以发送您的阵列吗?
  • 导出类 CapacitorBankFileComponent 实现 OnInit {capacitorBanks = []; this.subscriptions = this.cbank.getAssetServerAttributes(this.localCustomer, data[indexx]).subscribe(vres => { this.serverAttributes.push(vres[indexx]); indexx ++; });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 2014-08-19
  • 1970-01-01
  • 2016-02-07
  • 1970-01-01
相关资源
最近更新 更多