Angular指令转换中断布局

时间:2016-01-21 22:20:49

标签: angularjs angularjs-directive angularjs-ng-repeat angularjs-ng-transclude

我试图在ng-repeat循环中转换表行(a),以便在用户单击(a)时显示/隐藏另一个表行(b)。两个表行都在渲染,但是<ng-transclude>元素 - 在指令模板中包含(a)之前(b)需要打破布局。如何在不破坏布局的情况下使用指令呈现两个表行?

//主模板

<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>

2 个答案:

答案 0 :(得分:0)

我通过简单地使用ng-repeat-start / end(参见上面的更新)而不是指令来解决问题。

答案 1 :(得分:0)

它需要您稍微重新考虑您的指令,但您可以使用ng-repeat-startng-repeat-end为每个TR次迭代渲染多个ng-repeat,如下所示:< / p>

 <tr ng-repeat="vehicle in campaign.vehicles" 
     ng-click='vehicle.showDetails = !!!vehicle.showDetails'>
   <td> {{ vehicle.name }} </td>
 </tr>
 <tr ng-repeat-end ng-show='vehicle.showDetails'>
   <td> {{ vehicle.details }}
   </td>                        
 </tr>