小编典典

如何在TypeScript中迭代JSON属性

json

我的问题很简单,我有一个以下JSON Typescript对象,并且我想迭代遍历JSON属性

{"work_type":"Fabricación","work_type_resp":"Mesa","looking_for":"Relación Calidad/Precio","image":"https://s3-sa-east-1.amazonaws.com/online-quotation- images/153366018967.png","width":"22","high":"34","depth":"","modifications":"mod"}

使用的代码是以下有角TypeScript组件:

export class DetailsOnlineQuoteModalComponent implements OnInit {

  @Input() title;
  @Input() values:string;
  properties:JSON;

  constructor(public activeModal: NgbActiveModal) { }

  ngOnInit() {
    this.properties=JSON.parse(this.values);
    console.log(this.properties);
  }

}

我真正需要的是遍历JSON的键值属性,并在我的HTML中显示它们。

非常感谢!


阅读 1393

收藏
2020-07-27

共1个答案

小编典典

如果您使用的是Angular 6.1,请使用键值管道

<div *ngFor="let title of data | keyvalue">
  {{title.key}}
</div>

对于Angular 5

<div *ngFor="let key of dataKeys">
  {{key}} ------------- {{data[key]}}
</div>
get dataKeys() { return Object.keys(this.data); }

示例:https//stackblitz.com/edit/keyvalue-pipe-
qtaqnm

2020-07-27