angular.js:如果expression为true,则删除html元素

时间:2015-11-25 14:05:34

标签: javascript angularjs angularjs-ng-repeat html-table remove-if

我的控制器中有Collection

$scope.EventMarketCollection = [
        {
            Id: 1,
            EventMarketName: 'Full Time Result',
            OddsKey: 108,
            EventMarketOutcomes: [
                {
                    Id: 1,
                    OutcomeName: '1',
                    Odd: 3.25,
                    SumStake: 10,
                    BetSlipCount: 2
                },
                {
                    Id: 2,
                    OutcomeName: 'X',
                    Odd: 2.05,
                    SumStake: 110,
                    BetSlipCount: 1
                },
                {
                    Id: 3,
                    OutcomeName: '2',
                    Odd: 1.50,
                    SumStake: 21,
                    BetSlipCount: 5
                }
            ]
        }
    ];

我必须像这样绘制html表:

<style type="text/css">
  table td {
    text-align: center;
    vertical-align: middle;
  }
  </style>

<table border="1">
  <tbody>
    <tr>
      <td>Full Time Result</td>
      <td>Odd</td>
      <td>Odds Key</td>
      <td>Stake Sum</td>
      <td>BetSlip Count</td>
    </tr>
    <tr>
      <td>1</td>
      <td>3.25</td>
      <td rowspan="3">108</td>
      <td>10</td>
      <td>2</td>
    </tr>
    <tr>
      <td>X</td>
      <td>2.05</td>
      <td>110</td>
      <td>1</td>
    </tr>
    <tr>
      <td>2</td>
      <td>1.50</td>
      <td>21</td>
      <td>5</td>
    </tr>
  </tbody>
</table>

但是当我正在进行角度ng-repeat和rowspan =“something”时,我无法移除额外的<td>标签,我的输出看起来像:

<table border="1">
      <tbody>
        <tr>
          <td>Full Time Result</td>
          <td>Odd</td>
          <td>Odds Key</td>
          <td>Stake Sum</td>
          <td>BetSlip Count</td>
        </tr>
        <tr>
          <td>1</td>
          <td>3.25</td>
          <td rowspan="3">108</td>
          <td>10</td>
          <td>2</td>
        </tr>
        <tr>
          <td>X</td>
          <td>2.05</td>
          <td rowspan="3">108</td>
          <td>110</td>
          <td>1</td>
        </tr>
        <tr>
          <td>2</td>
          <td>1.50</td>
          <td rowspan="3">108</td>
          <td>21</td>
          <td>5</td>
        </tr>
      </tbody>
    </table>

这是我的ng-repeat代码:

<table class="table table-bordered">
  <tbody ng-repeat="eventMarket in EventMarketCollection">
    <tr>
      <td>{{eventMarket.EventMarketName}}</td>
      <td>Odd</td>
      <td>Odds Key</td>
      <td>Stake Sum</td>
      <td>BetSlip Count</td>
    </tr>
    <tr ng-repeat="eventMarketOutcomeItem in eventMarket.EventMarketOutcomes">
      <td>{{eventMarketOutcomeItem.OutcomeName}}</td>
      <td>{{eventMarketOutcomeItem.Odd}}</td>
      <td rowspan="{{eventMarket.EventMarketOutcomes.length}}" >{{eventMarket.OddsKey}}</td>
      <td>{{eventMarketOutcomeItem.SumStake}}</td>
      <td>{{eventMarketOutcomeItem.BetSlipCount}}</td>
    </tr>
  </tbody>
</table>

更新:您可以看到输出图像我想要做什么以及它目前是如何工作的 it outputs this

But, must output this

1 个答案:

答案 0 :(得分:0)

使用ngIf仅在第一行添加元素:

<td ng-if="$index === 0" rowspan="{{...}}" >{{eventMarket.OddsKey}}</td>

您的示例应如下所示:

&#13;
&#13;
angular.module('app', []).controller('Ctrl', function($scope) {
  
  $scope.EventMarketCollection = [{
    Id: 1,
    EventMarketName: 'Full Time Result',
    OddsKey: 108,
    EventMarketOutcomes: [{
      Id: 1,
      OutcomeName: '1',
      Odd: 3.25,
      SumStake: 10,
      BetSlipCount: 2
    }, {
      Id: 2,
      OutcomeName: 'X',
      Odd: 2.05,
      SumStake: 110,
      BetSlipCount: 1
    }, {
      Id: 3,
      OutcomeName: '2',
      Odd: 1.50,
      SumStake: 21,
      BetSlipCount: 5
    }]
  }];

})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="app" ng-controller="Ctrl">
  <table class="table table-bordered">
    <tbody ng-repeat="eventMarket in EventMarketCollection">
      <tr>
        <td>{{eventMarket.EventMarketName}}</td>
        <td>Odd</td>
        <td>Odds Key</td>
        <td>Stake Sum</td>
        <td>BetSlip Count</td>
      </tr>
      <tr ng-repeat="eventMarketOutcomeItem in eventMarket.EventMarketOutcomes">
        <td>{{eventMarketOutcomeItem.OutcomeName}}</td>
        <td>{{eventMarketOutcomeItem.Odd}}</td>
        <td ng-if="$index === 0" rowspan="{{eventMarket.EventMarketOutcomes.length}}">{{eventMarket.OddsKey}}</td>
        <td>{{eventMarketOutcomeItem.SumStake}}</td>
        <td>{{eventMarketOutcomeItem.BetSlipCount}}</td>
      </tr>
    </tbody>
  </table>
</body>
&#13;
&#13;
&#13;