Angular2 - ng-if有2个区块?

时间:2016-11-24 12:13:33

标签: angular

我有一个角度2的问题,我无法解决:(我试图显示web.api的数据。这有效 - 但我想创建一个带有4列的引导部分,如下所示。因此我需要每4个项目更改一次html。我每4个项目需要1个Block:

<div class="row">

和4个项目之后的一个:

    </div> <!-- to close the open row -->
<div class="row">
  <br />
</div>

但是如果我尝试用ng-if来做这件事 - 内部的整个区块都被吹灭了 - 如果我尝试这样做的话

    <template [ngIf]="(i+1)%4 == 0">
  <div class="row">
</template>

我收到未关闭标签的错误:(

有解决方案吗?我会很开心的。我希望我能充分描述我的问题。

&#13;
&#13;
             <div class="container" *ngFor="let item of _DataInterface; let i = index">
                <div class="row">
                    <div class="col-sm-3 col-md-3">
                        <div class="row box">
                            <div class="col-xs-3 col-sm-3 col-md-3 picture">
                            </div>
                            <div class="col-xs-9 col-sm-9 col-md-9 detail">
				Test1
                             </div>
                        </div>
                    </div>
                    <div class="col-sm-3 col-md-3">
                        <div class="row box">
                            <div class="col-xs-3 col-sm-3 col-md-3 picture center-block">
                            </div>
                            <div class="col-xs-9 col-sm-9 col-md-9 detail">
				Test2
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-3 col-md-3">
                        <div class="row box">
                            <div class="col-xs-3 col-sm-3 col-md-3 picture center-block">
                            </div>
                            <div class="col-xs-9 col-sm-9 col-md-9 detail">
				Test3
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-3 col-md-3">
                        <div class="row box">
                            <div class="col-xs-3 col-md-3 picture">
                            </div>
                            <div class="col-xs-9 col-md-9 detail">
				Test4
                            </div>
                        </div>
                    </div>

                </div><!-- close open row -->

                <div class="row">
                    <br />
                </div>

		<!-- next row -->
		<div class="row">
			<!-- next 4 items .... -->

           <!-- again a little distance -->
          <div class="row">
              <br />
          </div>
        </div>
	</div><!-- container close -->
&#13;
&#13;
&#13;

更新

<ng-container *ngIf="(i+1)%4 == 0"> 
  <div class="row"> 
  <br /> 
</ng-container> 

<ng-container *ngIf="(i+1)%4 == 1"> </div> 

</ng-container>

1 个答案:

答案 0 :(得分:5)

您可以使用<ng-container><ng-container>是一个永远不会添加到DOM的“虚拟元素”:

<ng-container *ngIf="expr">
  <!-- content here -->
</ng-container>