【问题标题】:ng repeat does not return variable from JSON fileng repeat 不从 JSON 文件返回变量
【发布时间】:2016-04-26 14:51:03
【问题描述】:

我有以下属于 AngularJS 框架中的模板的 html 代码:

   <ul class="sb-parentlist">
    <h1 class={{headerClass}}> Popular</h1><div data-ng-repeat="data in data track by $index">
    <li>
        <span class="sb-text-title"  href="#" ng-click="openFaq = ! openFaq"><b>{{data[$index].faq.frage |translate}}</b></span>
                    <span ng-show="openFaq" class="sb-text">
                         <br>
                         {{data[$index].faq.antwort |translate}}
                    </span>
    </li>
        </div>
    </ul>

在打印结果时,我在浏览器上正确获取了“li”元素的数量,但变量未按应有的方式定义,出现空白条目。 这是 JSON 条目:

   {

      "faq":
        {"frage":"HB_START_FAQ_Q",
          "antwort":"HB_START_FAQ_A"}
      ,
      "screencast":"HB_START_SCREENCAST"
    },
    {

      "faq":
        {"frage":"HB_START_FAQ_Q_1",
          "antwort":"HB_START_FAQ_A_1"}
      ,
      "screencast":"HB_START_SCREENCAST_1"
    },
    {

      "faq":
        {"frage":"HB_START_FAQ_Q_2",
          "antwort":"HB_START_FAQ_A_2"}
     ,
      "screencast":"HB_START_SCREENCAST_2"
    },
    {

      "faq":
        {"frage":"HB_START_FAQ_Q_3",
          "antwort":"HB_START_FAQ_A_3"}
     ,
      "screencast":"HB_START_SCREENCAST_3"
    }

我有兴趣获得嵌套项目。有什么想法吗?

【问题讨论】:

    标签: html angularjs json ng-repeat


    【解决方案1】:

    因为 data 在集合名称和被迭代的项目之间不明确 - 更改您的 ngRepeat 语法:

    data-ng-repeat="item in data track by $index"
    

    并使用item[$index]。我不完全确定你为什么不只是在做data.faq - 你需要选择$index

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      相关资源
      最近更新 更多