我正在尝试使用angularJS打印出一些数据。下面是脚本的相关段:由ng-repeat创建的额外div
$scope.incidentCats = [{
name: "FIRE",
incidents: [{
location: "location 1",
dateTime: "datetime 1",
currStatus: "currStatus 1"
},
{
location: "location 2",
dateTime: "datetime 2",
currStatus: "currStatus 2"
}]
},
{
name: "CODE BLUE",
incidents: [{
location: "location 3",
dateTime: "datetime 3",
currStatus: "currStatus 3"
}]
}];
正如你可以在数据看,事件类型FIRE
有两个事件的记录,而事件类型CODE BLUE
只有一个。
这里是在EJS文件中的相关代码:
<div ng-repeat="category in incidentCats" class="incident">
<header class="category">
<strong>{{ category.name }}</strong>
</header>
<section>
<div ng-repeat="incident in category.incidents">
{{ incident.location }}<br>
{{ incident.dateTime }}<br>
{{ incident.currStatus }}<br>
</div>
</section>
</div>
最后,这是我的HTML的这个特定部位(不包括用于整个项目,是从我手中的公用CSS)CSS:
.incident {
width: 100%;
height: 100%;
border: 1px solid #fff;
position: relative;
padding: 5px;
margin: 25px 0;
}
.incident section {
margin-top: 10px;
}
.incident section > div {
margin: 5px auto;
}
到目前为止,代码工作预期,除了一两件事:当事件循环中创建了div
元素,我期待2 div
为FIRE
类别中创建,只有一个div
为CODE BLUE
类别。但是,代码为这两个类别创建了两个div
。
这不是我想要的。由于第二个div
在CODE BLUE
中包含<br>
标签但没有数据,因为此类别的记录中没有第二次事件,所以现在有一段空白空间,其中第二个div
位于第一个div
之下,这很好因为第一个事件确实存在并被正确打印出来。我该如何纠正这一点?
您正在使用的AngularJS的版本? –
我刚刚用你的代码创建了一个[plunkr](https://plnkr.co/edit/rtjnZp1UVuyHUtddqiDi?p=preview),它似乎工作正常。 – th1rdey3