AngularJS - ng-repeat只显示一次标题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AngularJS - ng-repeat只显示一次标题相关的知识,希望对你有一定的参考价值。

我有2个列表(personsanimals),我用ng-repeat显示。我想在一个带有标题的表中显示这两个列表。以下是我想要显示数据的示例。我已经用ng-show="$first"尝试过了,但这不是我想要实现的结果。

enter image description here

<thead>
	<tr>
		<th>ID</th>
		<th>BIRTHDAY</th>
		<th>NAME</th>
	</tr>
</thead>

<tbody>
	<tr ng-repeat="ps in $ctrl.persons" ng-if="$ctrl.valuePersons == true">
		<th ng-show="$first">PERSONS</th>
			<td>{{ ps.id }}</td>
			<td>{{ ps.birthday | date: 'dd.MM.yyyy'}}</td>
			<td>{{ ps.name }}</td>
	</tr>
	
	<tr ng-repeat="an in $ctrl.animals" ng-if="$ctrl.valueAnimals == true">
		<th ng-show="$first">ANIMALS</th>
			<td>{{ an.id }}</td>
			<td>{{ an.birthday | date: 'dd.MM.yyyy'}}</td>
			<td>{{ an.name }}</td>
	</tr>
<tbody>
答案

根据ng-repeat的文档,这将有效。

...
<tr ng-repeat-start="ps in $ctrl.persons" ng-if="$ctrl.valuePersons == true && $first">
    <th>PERSONS</th>
    <td></td>
    <td></td>
</tr>
<tr ng-repeat-end ng-if="$ctrl.valuePersons == true && !$first">
    <td>{{ ps.id }}</td>
    <td>{{ ps.birthday | date: 'dd.MM.yyyy'}}</td>
    <td>{{ ps.name }}</td>
</tr>
...
另一答案

这很长但可以帮助你......

<table>
  <tr>
   <th>ID</th>
   <th>BirthDay</th>
   <th>NAME</th>
  </tr>
   <tr>
    <td>Persons</td>
    <td></td>
    <td></td>
   </tr>
   <tr ng-repeat="ps in $ctrl.persons">
     <td>{{ps.id}}<td/>
     <td>{{ps.bday}}<td/>
     <td>{{ps.name}}<td/>
   </tr>
<tr>
    <td>Animals</td>
    <td></td>
    <td></td>
 </tr>
   <tr ng-repeat="ps in $ctrl.animals">
     <td>{{ps.id}}<td/>
     <td>{{ps.bday}}<td/>
     <td>{{ps.name}}<td/>
   </tr>

</table>
另一答案

var app = angular.module("Profile", [] );
                app.controller("ProfileCtrl", function($scope) {
                        $scope.items    = [{'title':'PERSON','data':[{'birth_day':'1/1/2011','name':'a1'},{'birth_day':'1/1/2011','name':'a2'},{'birth_day':'1/1/2011','name':'a3'}]},{'title':'ANIMALS','data':[{'birth_day':'1/1/2011','name':'a1'},{'birth_day':'1/1/2011','name':'a2'},{'birth_day':'1/1/2011','name':'a3'}]}]
                })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="Profile" ng-controller="ProfileCtrl">
                <table class="table" border="1">
                        <thead>
                                <tr>
                                        <th>ID</th>
                                        <th>BIRTHDAY</th>
                                        <th>Name</thu>
                                </tr>
                        </thead>
                        <tbody>
                                <tr ng-repeat-start="item in items" ng-init="main_ind= $index">
                                        <td  colspan="3">{{item['title']}}</td>
                                </tr>
                                <tr ng-repeat-end ng-repeat="val in item['data']">
                                        <td>{{$index+1}}</td>
                                        <td>{{val['birth_day']}}</td>
                                        <td>{{val['name']}}</td>
                                </tr>
                        </tbody>
                </table>
        </body>

以上是关于AngularJS - ng-repeat只显示一次标题的主要内容,如果未能解决你的问题,请参考以下文章

如何在AngularJS中使用ng-repeat创建嵌套结构[关闭]

如何使用ng-repeat使bxSlider在AngularJS中工作?

AngularJS——创建自定义数据绑定和 ng-repeat

angularJS如何计算输入元素中显示的ng-repeat数据

ng-repeat 后的 AngularJS NanoScroller 不显示

嵌套 ng-repeat 的 AngularJS 记录计数