javascript - 如何在 Angular 2 中使用 *ngFor 打印列表中的 json 数组值?

标签 javascript arrays json angular

我有一个 json 数组,我想打印一个简单列表中的值。如何打印数值?我必须遵循键值方法吗?我正在添加 Json 数组结构和示例代码。它向我显示错误。

预期输出

*456
*123

or Arun :123 
test:456

错误:

"'[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

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

Json 数组

   {
        "records": [{
            "arun": [1, 2, 3],
            "test": [4, 5, 6]
        }]
    }

**myTest.ts**

export class HomePageComponent extends AppComponent {

  public bundle:  any[];

   ngOnInit(){
     this.readArray();
   }

   readArray() {
    this.bundle =  {
        "records": [{
            "arun": [1, 2, 3],
            "test": [4, 5, 6]
        }]
    }
    console.log("array",this.bundle);
   }

testComponent.html

<ul *ngFor="let det of bundle;">
  <li>{{det.records}}</li>
</ul>

最佳答案

您需要在数组上使用 ngFor,它应该是 <div *ngFor="let record of bundle.records"> ,还有一个嵌套数组,您可以使用以下方式访问项目 <强> Object.keys 如演示中所示

@Component({
  selector: 'my-app',
  template: `
   <div *ngFor="let record of bundle.records">
      <ul *ngFor="let key of objectKeys(record)">
         <li>{{key}} :: {{record[key]}}</li>
      </ul>
   </div>
  `,
})

<强> DEMO

关于javascript - 如何在 Angular 2 中使用 *ngFor 打印列表中的 json 数组值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46578427/

相关文章:

javascript - Youtube 数据 API 和谷歌云端点

javascript - 选择中选项更改的重置按钮和 Bootstrap 验证

javascript - AngularJS 使用循环向对象插入新属性

c++ - 如何使用模板为 "using"声明 "std::array"

java - 从 Twitter JSON Feed 中提取信息

jquery - __RequestVerificationToken 未使用 axios 0.17.1 和 MVC 3/4/5 正确发送

php - 如何检查一个对象是否为空?

javascript - reactjs:未捕获类型错误:无法读取未定义的属性 'value'

javascript - 递归问题。如何解决这个问题?

json - 使用 Groovy JsonBuilder 时如何保留时区?