我正在尝试在 ng-repeat 循环中嵌入表格行 (a),以便在用户单击 (a) 时可以显示/隐藏另一个表格行 (b)。两个表格行都在渲染,但是,<ng-transclude>
在指令模板中需要在 (b) 之前包含 (a) 的元素正在破坏布局。如何在不破坏布局的情况下使用指令呈现两个表格行?
// 主模板
<tr id="a" campaign-item ng-if="vm.campaignData.length > 0" ng-repeat="campaign in vm.campaignData | orderBy:vm.sortBy:vm.sortReverse | filter:searchCampaigns track by $index" ng-click="vm.showCampaignPreview(campaign)">
<td>{{ campaign.name }}</td>
<td>{{ campaign.priority }}</td>
<td>{{ campaign.status }}</td>
<td>{{ campaign.creator }}</td>
<td>{{ campaign.approver }}</td>
<td>{{ campaign.release_date }}</td>
<td>{{ campaign.expiration_date }}</td>
<td><select ng-init="campaignOptions = vm.campaignOptions[0]" name="campaignOption" class="form-control" ng-model="campaignOptions" ng-options="campaign.name for campaign in vm.campaignOptions track by campaign.value"></select></td>
</tr>
// 指令
(function(){
'use strict';
angular.module('vsmsCampaignModule')
.directive('campaignItem', campaign)
function campaign(){
var directive = {
link: link,
restrict: 'EA',
transclude: true,
templateUrl: 'app/vsms/admin/campaign/campaign.tpl.html'
};
return directive;
function link(scope, el, attr, ctrl, transclude) {
// do something with clone compiled and linked
// in child scope of directive's scope:
}
}
})();
// 指令模板
<ng-transclude></ng-transclude>
<tr id="b">
<td colspan="8">{{ campaign.description }}</td>
<td colspan="8">
<div class="row">
<div class="col-lg-1">
<ul>
<li ng-repeat="package in campaign.packages">
{{ package.name }}
</li>
</ul>
</div>
<div class="col-lg-1">{{ campaign.policy }}</div>
<div class="col-lg-1">{{ campaign.region }}</div>
<div class="col-lg-1">
<ul>
<li ng-repeat="vehicle in campaign.vehicles">
{{ vehicle.name }}
</li>
</ul>
</div>
</div>
</td>
</tr>
// 更新,我通过简单地使用 ng-repeat-start/end 解决了这个问题
<tr ng-class-odd="'odd'" ng-class-even="'even'" ng-if="vm.campaignData.length > 0" ng-repeat-start="campaign in vm.campaignData | orderBy:vm.sortBy:vm.sortReverse | filter:searchCampaigns track by campaign.id" ng-click="vm.showCampaignPreview(campaign)">
<td>{{ campaign.name }}</td>
<td>{{ campaign.priority }}</td>
<td>{{ campaign.status }}</td>
<td>{{ campaign.creator }}</td>
<td>{{ campaign.approver }}</td>
<td>{{ campaign.release_date }}</td>
<td>{{ campaign.expiration_date }}</td>
<td>
<select ng-init="campaignOptions = vm.campaignOptions[0]" name="campaignOption" class="form-control" ng-model="campaignOptions" ng-options="campaign.name for campaign in vm.campaignOptions track by campaign.value">
</select>
</td>
</tr>
<tr class="campaign-preview" ng-class-odd="'odd'" ng-class-even="'even'" ng-repeat-end ng-show="vm.selectedCampaign == campaign.id">
<td colspan="8">{{ campaign.description }}</td>
</tr>