如何在angularjs

时间:2018-05-14 17:22:33

标签: javascript arrays angularjs json

我有一个低于json结构的自动搜索模块。我需要循环遍历json对象,并根据需要使用键和值。

我试过下面的代码。但是使用提供的json对象,我能够检索密钥,但不能检索值。

让我们说,对于firt,Json对象,我需要检索'Product Apple'。但我只能获得链接。

我尝试了response.data[key][0],但获得了完整的json对象。我可以知道我做错了吗。

我已在下面更新了plunker

[{
    "/folder1/folder2/folder3/product-1": "Product Apple"
  },
  {
    "/folder1/folder2/folder3/product-2": "Product samsung"
  },
  {
    "/folder1/folder2/folder3/product-3": "Product lenovo"
  },
  {
    "/folder1/folder2/folder3/product-4": "Product Asus"
  },
  {
    "/folder1/folder2/folder3/product-5": "Product Acer"
  },
  {
    "/folder1/folder2/folder3/product-6": "Product Vivo"
  },
  {
    "/folder1/folder2/folder3/product-7": "Product Oppo"
  }
]

code here

2 个答案:

答案 0 :(得分:0)

由于这被标记为重复发布。我已经通过提供的解决方案,发现重复的帖子通过javascript有解决方案。但我希望通过angularjs'ng-repeat'进行迭代。

请在下面找到有关解决方案的plunker

[code here][1]

code here

答案 1 :(得分:-1)

您可能希望将该结构分配给变量,然后在其上运行*ngFor,如下所示:

// in the component file
let results = [ 
   {  
      "/folder1/folder2/folder3/product-1":"Product Apple"
   },
   {  
      "/folder1/folder2/folder3/product-2":"Product samsung"
   },
   {  
      "/folder1/folder2/folder3/product-3":"Product lenovo"
   },
   {  
      "/folder1/folder2/folder3/product-4":"Product Asus"
   },
   {  
      "/folder1/folder2/folder3/product-5":"Product Acer"
   },
   {  
      "/folder1/folder2/folder3/product-6":"Product Vivo"
   },
   {  
      "/folder1/folder2/folder3/product-7":"Product Oppo"
   }
]

// in the view
<ng-container *ngFor="let result of results">
  view logic goes here
</ng-container>