我现在正在做一个项目,我们有几个不同的Tasks
,每个Task
都有自己的数据显示方式,它们是不需要单独控制器和视图的微小数据更改。
例如,一个任务可能有一个显示 的标题,Username
下一个任务不需要用户名并将其替换为Behavior
.
这是我现在正在做的事情:
<div id="event-list">
<ul>
<li ng-click="isHidden=!isHidden;" ng-repeat="event in events" ng-init="isHidden=false;">
<!-- Template 1 -->
<div ng-if="taskCategory == 'taskExampleOne'">
<div class="heading">
<h1>{{event.EventName}}</h1>
<p class="description">Username: {{ event.User.Name }}</p>
<p class="date">{{ event.RecordDateUtc | moment: 'MM/DD/YYYY' }}</p>
</div>
<div ng-show="isHidden" class="body event-show-hide-animation">
<img src="{{ event.SnapshotJpg }}" />
<dl>
<dt>Record Date: </dt>
<dd>{{ event.RecordDateUtc | moment: 'MM/DD/YYYY HH:MM A' }} {{ event.RecordDateTz }}</dd>
</dl>
<button type="button" class="btn btn-primary" ng-click="viewEventDetails( event )">View Event</button>
</div>
</div>
<!-- End Template 1 -->
<!-- Template 2 -->
<div ng-if="taskCategory == 'taskExampleTwo'">
<div class="heading">
<h1>{{event.EventName}}</h1>
<p class="description">
<span>Username: {{ event.User.Name }}</span>
<span>Behavior: {{ event.BehaviorNames }}</span>
</p>
<p class="date">{{ event.RecordDateUtc | moment: 'MM/DD/YYYY' }}</p>
</div>
<div ng-show="isHidden" class="body event-show-hide-animation">
<dl>
<dt>Event: </dt>
<dd>{{ event.EventId }}</dd>
<dt>Group: </dt>
<dd>{{ event.QueryKey.GroupIds[0] }}</dd>
<dt>Record Date: </dt>
<dd>{{ event.RecordDateUtc | moment: 'MM/DD/YYYY HH:MM A' }} {{ event.RecordDateTz }}</dd>
</dl>
<button type="button" class="btn btn-primary" ng-click="viewEventDetails( event )">View Event</button>
</div>
</div>
<!-- End Template 2 -->
</li>
</ul>
</div>
所以通过重复元素有相当多的浪费,但是我觉得拆分整个模板而不是ng-if
为每条信息设置大量条件更具可读性。
有没有更好的方法来处理这些模板?