以angular2 [重复]显示对象

时间:2021-11-16 11:20:25

This question already has an answer here:

这个问题在这里已有答案:

OK, I have this

好的,我有这个

{ "calledCust": { "no": 270, "yes": 9 }, "hasOpened": { "no": 254, "yes": 25 } }

and I have been trying this to display in the view.

我一直试图在视图中显示。

<ul *ngFor="let profile of profiles.counts">
  <li>
    {{profile | json}}
  </li>
</ul>

because it is not a json array, angular just blows up with

因为它不是一个json数组,所以角度只是爆炸了

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

and I really want to display the results as below.

我真的想显示如下结果。

  • calledCust YES:9
  • 叫做:是的:9
  • hasOpened YES:25
  • hasOpened是:25

Many thanks

非常感谢

EDIT

编辑

here is the full json

这里是完整的json

{
    total_rows: 279,
    bookmark: "g2wAAAABaANkAB5kYmNvcmVAZGI0LnBvcnRlci5jbG91ZGFudC5uZXRsAAAAAmEAYj____9qaAJGQAnZi4AAAABiAAAB7mo",
    rows: [],
    counts: {
        calledCust: {
            no: 270,
            yes: 9
        },
        hasOpened: {
            no: 254,
            yes: 25
        }
    }
}

1 个解决方案

#1


0  

You just need to convert it to a JSON array, something like:

您只需要将其转换为JSON数组,例如:

let key; // var key for old JavaScript ES5
let arr = [];

key = 'calledCust';
arr[0] = obj[key];
arr[0].key = key;

key = 'hasOpened';
arr[1] = obj[key];
arr[1].key = key;

would be easy to create a pipe that will take your object and covert it to a JSON array then you would just do:

可以很容易地创建一个管道来获取你的对象并将其转换为JSON数组然后你会这样做:

<ul *ngFor="let profile of profiles.counts | convertObjToJsonArray">

You can iterate over the properties of an object to make this solution more generic.

您可以迭代对象的属性以使此解决方案更通用。

#1


0  

You just need to convert it to a JSON array, something like:

您只需要将其转换为JSON数组,例如:

let key; // var key for old JavaScript ES5
let arr = [];

key = 'calledCust';
arr[0] = obj[key];
arr[0].key = key;

key = 'hasOpened';
arr[1] = obj[key];
arr[1].key = key;

would be easy to create a pipe that will take your object and covert it to a JSON array then you would just do:

可以很容易地创建一个管道来获取你的对象并将其转换为JSON数组然后你会这样做:

<ul *ngFor="let profile of profiles.counts | convertObjToJsonArray">

You can iterate over the properties of an object to make this solution more generic.

您可以迭代对象的属性以使此解决方案更通用。