简体   繁体   English

显示 Json object 属性

[英]Show Json object properties

In my application I have json array and I can display those data.在我的应用程序中,我有 json 数组,我可以显示这些数据。 all data have radio button.所有数据都有单选按钮。 what I want is when I click on radio button, data of that json array item will print in html format in another component.我想要的是当我单击单选按钮时,该 json 数组项的数据将以 html 格式打印在另一个组件中。 Something like below.像下面的东西。 This is how it is showing这就是它的显示方式

withing my product.component.ts I have below.使用我的 product.component.ts 我在下面。

ngOnInit() {
   this.wordpressService.apiCall().subscribe((data)=>{
     console.warn("get api data ",data);
     this.title=data;
   })
}
radioHandle(event : any){
     this.post=event.target.value;
  }

Then within my product.component.html I display some json data as below.然后在我的 product.component.html 中显示一些 json 数据,如下所示。

<div class='container'>
  <div id="leftDiv">
   <div *ngFor="let data of title">
    <p>
      <input type="radio"
      name="posts"
      value="{{data | json}}"
      (change)="radioHandle($event)">
      {{data.id}}
    </p>
  </div>
</div>
<div id="rightDiv">
  <app-productDisplay [parentData]=post></app-productDisplay>
</div>
</div>

now I want to pass that selected data to another component call productDisplay.component.ts现在我想将选定的数据传递给另一个组件调用 productDisplay.component.ts

 @Input() public parentData: any;

And display the data within productDisplay.component.html并显示productDisplay.component.html中的数据

{{parentData}}

It works.有用。 But the problem is it is display json object.但问题是显示 json object。 But I want to print some properties of json object instead of whole json但我想打印 json object 而不是整个 json 的一些属性

Have you tried the JSON.parse()?您是否尝试过 JSON.parse()? I guess that could do what you described.我想这可以做你描述的。

In your productDisplay.component.ts:在您的 productDisplay.component.ts 中:

let parentDataParsed = JSON.parse(parentData)

And in your template:在您的模板中:

{{parentDataParsed.Id}}

You have to parse the JSON first in product.component.ts您必须首先在 product.component.ts 中解析 JSON

ngOnInit() {
   this.wordpressService.apiCall().subscribe((data)=>{
     console.warn("get api data ",data);
     this.parentDataParsed = JSON.parse(data)
   })
}

Pass the data to the productDisplay component by data binding通过数据绑定将数据传递给productDisplay组件

<app-product-display [parentData]="parentDataParsed"></app-product-display>

In the productDisplay component, you can show individual fields在 productDisplay 组件中,您可以显示单个字段

<p>ID: {{parentData?.id}}</p>
<p>TITLE: {{parentData?.title}}</p>
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM