AngularJS ng-repeat in tbody not working

时间:2016-07-24 09:33:11

标签: javascript angularjs

我在tbody中使用ng-repeat有问题。 当我使用该代码时:

<tbody>
   <tr ng-repeat="group in groups">
      <td>{{ group.name }}</td>
   </tr>
</tbody>

我收到此错误:

  

TypeError:无法读取属性&#39; childNodes&#39;未定义的

但是有了这段代码:

<div ng-repeat="group in groups">{{group.name}}</div>

在同一范围内工作完美。我已经找到了在tbody上使用ng-repeat的一些答案,但我需要这个结构来使用jquery数据表。有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

尝试这样,它的工作

&#13;
&#13;
var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
   $scope.groups = [
        {name: 'Michał', users:  5, owners: 4},
        {name: 'Michał', users:  5, owners: 4},
        {name: 'Michał', users:  5, owners: 4},
        {name: 'Michał', users:  5, owners: 4}
    ];
});
&#13;
<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link href="style.css" rel="stylesheet" />
    <script data-semver="1.4.9" src="https://code.angularjs.org/1.4.9/angular.js" data-require="angular.js@1.4.x"></script>
    <script src="app.js"></script>
  </head>

  <body ng-app="plunker" ng-controller="MainCtrl">
    using div
  <div ng-repeat="group in groups">{{group.name}}</div>
  using table
   <table>
  <tbody>
  <thead>
                        <tr>
                            <th>name</th>
                            <th>user</th>
                            
                        </tr>
                    </thead>  
 <tr ng-repeat="group in groups">
      <td>{{ group.name }}</td>
       <td>{{ group.users }}</td>
   </tr>
  </tbody>
</table> 

  </body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您正尝试访问“group.name”

如果“groups”是您的范围数组名称,请确保“name”是您的数组元素。 根据您获得的错误,我假设您的数组元素名称与您尝试访问的不一样。 例如:

app.js:

var app = angular.module('testing', []);

app.controller('mainctrl', function($scope) {
$scope.groups = [
{name: 'Apple'},
{name: 'Orange'},
{name: 'Banana'}
];
});

的index.html

<!DOCTYPE html>
<html ng-app="testing">
<head>
<meta charset="utf-8" />
<title>Testing</title>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
</head>
<body ng-controller="mainctrl">
<table>
  <tr ng-repeat='group in groups'>
    <td>{{group.name}}</td>
  </tr>
  </table>
  </body>
  </html>
希望它有所帮助。 :)