Angularjs - 如何正确使用ng-repeat

时间:2016-03-02 14:28:00

标签: angularjs angularjs-ng-repeat ng-repeat

我在角度应用上使用ng-repeatng-switch,但我对它们的打印方式有一个小问题。

这是代码,因此更清晰:

    <div class="test" ng-repeat="data in dataTest track by data.id" ng-switch="data.id">
      <div ng-switch-when="0">
         <p>{{data.id}}</p>
       </div>
    </div>

转发器工作,问题是我将此结果打印为最终结果:

<div class="test" ng-repeat="data in dataTest track by data.id" ng-switch="data.id">
  <div ng-switch-when="0">
     <p>0</p>
  </div>
</div>

<div class="test" ng-repeat="data in dataTest track by data.id" ng-switch="data.id"></div>

<div class="test" ng-repeat="data in dataTest track by data.id" ng-switch="data.id"></div>

<div class="test" ng-repeat="data in dataTest track by data.id" ng-switch="data.id"></div>

<div class="test" ng-repeat="data in dataTest track by data.id" ng-switch="data.id"></div>

<div class="test" ng-repeat="data in dataTest track by data.id" ng-switch="data.id"></div>

这里的问题非常明显:我不想要所有那些空div,但我只想要一个,匹配ng-switch-when匹配id = 0的那个。

我该怎么做?放置ng-repeat的最佳位置在哪里,以便它不打印空容器?

我尝试将它移动到不同的位置,但我最终总是创建了空元素。

1 个答案:

答案 0 :(得分:1)

您可以使用ng-if,如下所示 -

<div class="test" ng-repeat="data in dataTest track by data.id" ng-if="data.id==0">
      <div >
         <p>{{data.id}}</p>
       </div>
    </div>