json - 按特定 JSON 值过滤以绑定 Angular 5
问题描述
我有一个具有这方面的 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++;
});
非常感谢!
解决方案
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>
推荐阅读
- gcc - 未定义 _GNU_SOURCE 时 GNU 扩展功能如何运行
- mysql - 最佳实践使用 mysql 在节点 js 上建立连接
- python - 将csv导入mysql:文件名作为列中的值
- css - 使用 HTML5 和 Bootstrap Select 选择不同的高度
- record - 在 Pascal 中计算记录的唯一值
- java - 如何在 Intellij Idea 中自定义方法存根?
- java - 在 Java 中从 base10 值转换为 base60
- postscript - 后记测试条件
- php - Php:检查是否有查询以及是否传递了 GET 参数的可靠方法
- ruby-on-rails - Rails 控制器参数通过干式验证进行验证