4

有人可以帮我吗?下面是我的代码和问题解释。

<table>
 <thead>
   <tr>
     <th>Icon</th>
     <th>Name</th>
     <th>Details</th>
   </tr>
 </thead>

 <tbody ng-repeat="category in editor.categories">
    <tr>
      <th>{{category.categoryName}}</th>
    </tr>
    <tr ng-repeat="feature in category.features"> // Question?
       <td>{{feature.iconImg}}</td>
       <td>{{feature.featureName}}</td>
       <td>{{feature.details}}</td>
     </tr>
     <tr ng-if="feature.id==1"> // condition 1
     </tr>
     <tr ng-if="feature.id==2"> // condition 2
     </tr>
  </tbody>
</table>

现在,让我解释一下这个问题。我有一组类别。每个类别都作为一组特征。

现在我的问题是,如何根据ng-if条件显示每个类别的这些功能。即,<tr>仅当条件 1 时才应显示feature.id==1,并且仅当条件 2 时才显示feature.id==2

Q. 如何根据不同的条件包含我的 ng-if 条件?

非常感激你的帮助。

4

2 回答 2

3

在一个标签中同时使用 ng-if 和 ng-repeat:

<tr ng-repeat="feature in features"
    ng-if="feature.id === 1" >
</tr>

或者,如果你想有条件地显示一些模板,你可以使用 ng-switch 例如:

<tr
    ng-repeat="feature in features"
    ng-switch="feature.id" >
    <span ng-switch-when="1"></span>
    <span ng-switch-when="2"></span>
</tr>
于 2015-09-08T10:01:27.537 回答
3

检查 ng-switch 和 ng-switch-when

https://docs.angularjs.org/api/ng/directive/ngSwitch

问候

于 2015-09-08T10:11:27.007 回答