【问题标题】:Ng-Repeat does not repeat (or return anything)Ng-Repeat 不重复(或返回任何内容)
【发布时间】:2015-01-29 08:19:41
【问题描述】:

我是 AngularJS 的新手,目前仍在使用它。为了自学这些概念,我正在处理一些手工构建的 JSON 文件。

我首先使用它来构造弹出式 javascript 菜单的元素,方法是在控制器访问 JSON 文件的列表元素上使用 ng-repeat。那工作得很好。然后,我尝试使用稍微复杂的数据模型在不同的 JSON 文件上重复一个相当复杂的表行结构。在运行时,这会返回一条注释,就像之前的实验一样,但没有任何元素与之相伴。

我正在使用两个控制器作为一个应用程序的一部分。我认为问题可能在于如何访问 JSON 文件中的数据。

不包括 CSS,因为我不认为会影响它。 'MenuCtrl' 控制器仍然正常运行(在整个页面的元素上调用 ng-app),所以我只包括我认为相关的 HTML。

controllers.js:

  var profileApp = angular.module('profileApp', []);
  profileApp.controller('BookCtrl', function ($scope, $http){
    $http.get('books.json').success(function(data) {
      $scope.bookItems = data;
    });
  });
  profileApp.controller('MenuCtrl', function ($scope, $http){
    $http.get('profile.json').success(function(data) {
      $scope.profileItems = data;
    });
  });

books.json:

[
  {
"title": "The Design of Everyday Things",
"author": "Donald Norman",
"img": "img/norman.jpg",
"isbn": "",
"description":".",
"id1": "hoverA",
"id2": "hover1"
  },
  {
"title": "Plans and Situated Actions",
"author": "Lucille Suchman",
"img": "img/planssitu.jpg",
"isbn": "",
"description":".",
"id1": "hoverB",
"id2": "hover2"
  },
  {
"title": "Where The Action Is",
"author": "Paul Dourish",
"img": "img/wheretheaction.jpg",
"isbn": "",
"description":".",
"id1": "hoverC",
"id2": "hover3"
  },
  {
"title": "Information Processing and Human-Machine Interaction",
"author": "Jens Rasmussen",
"img": "img/rasmussen.jpg",
"isbn": "",
"description":".",  
"id1": "hoverD",
"id2": "hover4"
  },
  {
"title": "The Imaginary App",
"author": "Paul D. Miller and Svitlana Matviyenko",
"img": "img/imaginary.jpg",
"isbn": "",
"description":".",
"id1": "hoverE",
"id2": "hover5"
  },
  {
"title": "The Gutenberg Galaxy",
"author": "Marshall Mcluhan",
"img": "img/guten.jpg",
"isbn": "",
"description":".",
"id1": "hoverF",
"id2": "hover6"
  }
]

HTML 的相关部分:

<div  align="center">
<p class="central">Bibliography</br></p></br>
<table class="content1" ng-controller="BookCtrl" >
    <colgroup>
        <col span="1" class="col1">
        <col span="1.5" class="col2">
    </colgroup>
    <tbody ng-repeat="book in bookItems" >
    <tr >
        <td align="center"><div class="center"><div class="round3"><a href=""><img class="book" ng-src="{{book.img}}"></img></a></div></div></td>
        <td></td>
        <td class="wrapword" style="padding-left:10px;"><p><span class="content2"><strong>{{book.title}}</strong></span>
            <span class="small2">{{book.author}}</br>
            <span class="hide2" id="{{book.id1}"}>See Description</span>
            <span class="hide" id="{{book.id2}}">{{book.description}}</span>
            </span>
        </p></td>
    </tr>
    </tbody>
</table>
</div>

检查运行页面的元素时我得到了什么:

<table class="content1 ng-scope" ng-controller="BookCtrl">
    <colgroup>
        <col span="1" class="col1">
        <col span="1.5" class="col2">
    </colgroup>
    <!-- ngRepeat: book in bookItems -->
</table>

【问题讨论】:

  • 控制台有错误吗?
  • 一个问题是你在 tbody 元素上使用了ng-repeat,它应该定义一次。

标签: javascript html json angularjs ng-repeat


【解决方案1】:

你已经提到了,在 TBODY 中的 ng-repeat,我更正了。现在应该可以了。

<div  align="center">
<p class="central">Bibliography</br></p></br>
<table class="content1" ng-controller="BookCtrl" >
    <colgroup>
        <col span="1" class="col1">
        <col span="1.5" class="col2">
    </colgroup>
    <tbody  >
    <tr ng-repeat="book in bookItems">
        <td align="center"><div class="center"><div class="round3"><a href=""><img class="book" ng-src="{{book.img}}"></img></a></div></div></td>
        <td></td>
        <td class="wrapword" style="padding-left:10px;"><p><span class="content2"><strong>{{book.title}}</strong></span>
            <span class="small2">{{book.author}}</br>
            <span class="hide2" id="{{book.id1}"}>See Description</span>
            <span class="hide" id="{{book.id2}}">{{book.description}}</span>
            </span>
        </p></td>
    </tr>
    </tbody>
</table>
</div>

【讨论】:

    【解决方案2】:

    好的,我看到一些无效的标记,但我认为您的代码由于这个无效标记而中断:&lt;span class="hide2" id="{{book.id1}"}&gt;See Description&lt;/span&gt;,更改为 &lt;span class="hide2" id="{{book.id1}}"&gt;See Description&lt;/span&gt;

    【讨论】:

    • 这实际上是您建议的内容(我盯着这个看了一会儿,没听懂!)和下面的 Aviro 的建议。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-08-10
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    相关资源
    最近更新 更多