【问题标题】:I have a list of dictionaries in my json, I want to use it in angularjs我的 json 中有一个字典列表,我想在 angularjs 中使用它
【发布时间】:2016-05-27 10:16:05
【问题描述】:

我的 json 看起来像这样:

[  
{
    "statCount": 5,
    "inceptionStat": [
      {
        "1": "name1",
        "2": "name2",
        "3": "name3",
        "4": "name4",
        "5": "name5"
      }
    ]
  },  
{"statCount": 2,
 "inceptionStat": 
[
      {
        "1": "name1",
        "2": "name2"
      }
    ]
  },  
{
    "statCount": 0,
    "inceptionStat": [
      {}
    ]
  }

]

我想在 angularjs 中使用 inceptionStat 数组来创建我所有名字的列表,我尝试了多种方法,但我没有找到一种方法只为每个 statCount 打印 name1、name2 等。

我使用了一些 javascript 来隔离 inceptionStat 数据:

 $scope.result = $($scope.data).map(function () {
            return this.inceptionStat;
        }).get()
        console.log($scope.result);

$scope.data 这里是整个 json 数据。

所以从这里开始,我尝试使用 ng-repeat 从我的不同数组对象创建我的名称列表,但我没有找到我猜的正确方法。

有没有简单的方法来做到这一点?

【问题讨论】:

  • 你能告诉我如何你尝试使用ng-repeat吗?

标签: javascript angularjs json dictionary


【解决方案1】:

您可以像这样简单地使用 ng-repeat:

<div ng-repeat="dictionary in dictionaries">
  <h4>statcount: {{dictionary.statCount}}</h4>
  <ul>
    <li ng-repeat="(key, value) in dictionary.inceptionStat[0]">{{value}}</li>
  </ul>
</div>

使用 JSON:

$scope.dictionaries = [
  {
    "statCount": 5,
    "inceptionStat": [
      {
        "1": "name1",
        "2": "name2",
        "3": "name3",
        "4": "name4",
        "5": "name5"
      }
    ]
  },
  {
    "statCount": 2,
    "inceptionStat": [
      {
        "1": "name1",
        "2": "name2"
      }
    ]
  },
  {
    "statCount": 0,
    "inceptionStat": [
      {}
    ]
  }
];

inceptionStat[0] 中包含零,因为您的 inceptionStat 是一个对象,而不是一个数组。

【讨论】:

  • 就是这样,谢谢你的帮助。我错过了 [0] !
  • 没问题。很高兴为您提供帮助!
猜你喜欢
  • 2018-09-18
  • 2017-09-04
  • 1970-01-01
  • 1970-01-01
  • 2021-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-17
相关资源
最近更新 更多