简体   繁体   English

Angular-在JSON对象中显示对象

[英]Angular - Displaying Object within Object in json

I'm trying to use angular ngFor to parse this data: 我正在尝试使用angular ngFor解析此数据:

Link: https://blockchain.info/rawaddr/3MvuMn4CESuvA89bfA1cT8mgC4JtKaReku 链接: https//blockchain.info/rawaddr/3MvuMn4CESuvA89bfA1cT8mgC4JtKaReku

I'm able to retrieve the data fine by subscribing to it, but when I try to display attributes that has an object in it. 我可以通过订阅来很好地检索数据,但是当我尝试显示其中包含对象的属性时。 I receive [object, Object]. 我收到了[对象,对象]。

How I am currently attempting this 我目前如何尝试

<div *ngFor="let t of transactions" >
  <mat-card class="example-card">
    <mat-card-header>
      <mat-card-title></mat-card-title>
    </mat-card-header>
    <mat-card-content>
      <table class="table">
        <thead>
          <tr>
            <th scope="col">From</th>
            <th scope="col">To</th>
            <th scope="col">Receiver</th>
            <th scope="col">Hash</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>{{t.inputs}}</td>
            <td>-></td>
            <td>{{t.out}}</td>
            <td></td>
          </tr>
        </tbody>
      </table>

component.ts component.ts

export class InputComponent implements OnInit {

  address = "3MvuMn4CESuvA89bfA1cT8mgC4JtKaReku";
  transactions: any ;
  inputs: any;
  public arrayOfKeys
  constructor(private blockchain: BlockchainService) { }

  ngOnInit() {
  }

  submit(){
    console.log(this.address)
  }

  getTransactions(){
    this.blockchain.getTransactions().subscribe((data) => {
      this.transactions = data["txs"]
      console.log(this.transactions)
      console.log(this.transactions['inputs'])

    });
  }
}

A simple test with console.log to see if this.transactions['inputs'] gives me an undefined 使用console.log进行的简单测试,看this.transactions ['inputs']是否给我未定义的信息

New attempt below. 下面的新尝试。 It's closer to working but not fully there yet. 距离工作更近了,但还没有完全完成。

<div *ngFor="let t of transactions">
    <div *ngFor="let field of t.inputs ; let i = index">
        <p>{{ field.sequence }}</p>
        <p>{{ field.witness}}</p>
        {{i}}
        {{field.prev_out[i].addr}}
        <hr>
        <div *ngFor="let out of field.prev_out">
          {{out.addr}}
          </div> 
    </div>
</div>

The error it gives me 错误给我

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

Because inputs and out are objects. 因为inputsout都是对象。 You would have to do {{ t.inputs.fieldYouWant }} to get a certain field of the input object. 您将必须执行{{ t.inputs.fieldYouWant }}以获取input对象的特定字段。

If you want the entire input you would have to store it in your component and do an ngFor on that. 如果您需要整个input必须将其存储在组件中,然后对其执行ngFor

I'm not sure if you can do something like this but you can try it out: 我不确定您是否可以执行以下操作,但是可以尝试一下:

<div *ngFor="let t of transactions">
     <div *ngFor="let field of t.inputs">
         {{ field }}
     </div>
</div>

Well, its because the t.inputs and t.out are objects and not string. 好吧,因为t.inputst.out是对象而不是字符串。 If you want to just show the plain text data then you can use "json" filter of anuglarjs or convert these values in string using "JSON.stringify" 如果您只想显示纯文本数据,则可以使用anuglarjs的“ json”过滤器,或使用“ JSON.stringify”将这些值转换为字符串

Using angularjs json filter - 使用angularjs json过滤器-

<pre>{{ {'name':'value'} | json }}</pre>

The t.inputs is not an object, it is an array of object. t.inputs不是对象,它是对象的数组。 So I think this should work. 所以我认为这应该可行。

<div *ngFor="let t of transactions">
<div *ngFor="let field of t.inputs ; let i = index">
    <p>{{ field.sequence }}</p>
    <p>{{ field.witness}}</p>
    {{i}}
    <hr>
    {{field.prev_outt.addr}}
</div>
</div>

A simple test with console.log to see if this.transactions['inputs'] gives me an undefined 使用console.log进行的简单测试,看this.transactions ['inputs']是否给我未定义的信息

Quick Solution if you are getting the date in console instead of undefined than the approach of getting the data might be wrong. 快速解决方案,如果您要在控制台中获取日期而不是未定义日期,则获取数据的方法可能是错误的。 The data object in getTransactions() is of undefined type. getTransactions()的数据对象是未定义的类型。 Instead of t.out , assign value to a variable resp by writing t['out'] , giving complete name eg ['inputs']['0']['sequence'] in your component. 通过写t['out']而不是t.out ,将值赋值给变量resp ,在组件中给出完整的名称,例如['inputs']['0']['sequence'] Than use resp in component.html 比在component.html中使用resp

Solution

Cast the result data by defining interface for json and than use . 通过为json定义interface ,然后使用use来转换结果数据. to access the values 访问值

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

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