gpt4 book ai didi

javascript - ngRepeat 用作表内的元素不起作用

转载 作者:行者123 更新时间:2023-11-30 11:45:29 27 4
gpt4 key购买 nike

当 AngularJS 1.6.0(与旧版本相同)和 ngRepeat 指令用作元素嵌套在表格中时,我遇到了一个奇怪的问题。

简单地说<ng-repeat><table> , 然后放一个 <tr>内部:它不起作用。但是如果你把 <ng-repeat><table> 之外,例如。用<div>在内部,它完美地工作。

在我们的 buggy 案例中检查呈现的 HTML 时,我们可以看到 ng-repeat 的迭代,但它们超出了表格,从而破坏了 DOM 并使线条消失:

 <p>When placed inside a table, with tr inside, it doesn't work:</p>
<!-- ngRepeat: person in people --><ng-repeat ng-repeat="person in people" class="ng-scope">
</ng-repeat><!-- end ngRepeat: person in people --><ng-repeat ng-repeat="person in people" class="ng-scope">
</ng-repeat><!-- end ngRepeat: person in people --><ng-repeat ng-repeat="person in people" class="ng-scope">
</ng-repeat><!-- end ngRepeat: person in people -->
<table>
<tbody>
<tr>
<td class="ng-binding"></td>
</tr>
</tbody>
</table>

已使用最新的 Chrome 和 Firefox 进行测试。

angular.module('myApp', [])

.controller('MyCtrl', function($scope) {
$scope.people = [{
name: 'John',
age: 20
}, {
name: 'Peter',
age: 22
}, {
name: 'Jane',
age: 19
}];
});
<!DOCTYPE html>
<html ng-app="myApp">

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script>
<meta charset=utf-8 />
<title>ngRepeat</title>
</head>

<body>

<div ng-controller="MyCtrl">
<p>Hello ng-repeat!</p>

<p>With div inside, ng-repeat element works:</p>
<ng-repeat ng-repeat="person in people">
<div>
<td>{{person.name}}</td>
</div>
</ng-repeat>

<p>When ng-repeat element is placed inside a table, with tr inside, <strong>it doesn't work</strong>:</p>
<table>

<ng-repeat ng-repeat="person in people">
<tr>
<td>{{person.name}}</td>
</tr>
</ng-repeat>

</table>

<p>But, when ng-repeat is used as an attribute on tr, it works!</p>
<table>

<tr ng-repeat="person in people">
<td>{{person.name}}</td>
</tr>

</table>



</div>
</body>

</html>

我错过了什么吗?

最佳答案

ng-repeat 不是 table 标签中的有效元素,浏览器不会提示无法识别的属性。

tr,tbody,thead 是表格标签中的有效元素。

这里我只是将表格中的ng-repeat改成了tbody。那么它就起作用了。

angular.module('myApp', [])

.controller('MyCtrl', function($scope) {
$scope.people = [{
name: 'John',
age: 20
}, {
name: 'Peter',
age: 22
}, {
name: 'Jane',
age: 19
}];
});
<!DOCTYPE html>
<html ng-app="myApp">

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script>
<meta charset=utf-8 />
<title>ngRepeat</title>
</head>

<body>

<div ng-controller="MyCtrl">
<p>Hello ng-repeat!</p>

<p>With div inside, ng-repeat element works:</p>
<ng-repeat ng-repeat="person in people">
<div>
<td>{{person.name}}</td>
</div>
</ng-repeat>

<p>When ng-repeat element is placed inside a table, with tr inside, <strong>it doesn't work</strong>:</p>
<table>

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

</table>

<p>But, when ng-repeat is used as an attribute on tr, it works!</p>
<table>

<tr ng-repeat="person in people">
<td>{{person.name}}</td>
</tr>

</table>



</div>
</body>

</html>

关于javascript - ngRepeat 用作表内的元素不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41118052/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com