AngularJS if ng-repeat中的语句条件

时间:2014-06-09 13:43:53

标签: javascript angularjs angularjs-scope angularjs-ng-repeat angular-ui

我在AngularJS中使用ng-repeat

<ul>
    <li ng-repeat="question in questions" class="question list-group-item media">
        <span class="question__type">{{ question.question }}</span>
        <br>
        <div class="btn-group question__answer justified nav-tabs">
            <a class="btn {{question.type}}" ng-repeat="answer in question.answers">{{ answer }}</a>
        </div>
    </li>
</ul>

我希望能够将以下部分转换为if {{question.type}} == 'radio' then justified div上btn-group的if语句。

        <div class="btn-group question__answer justified nav-tabs">
            <a class="btn {{question.type}}" ng-repeat="answer in question.answers">{{ answer }}</a>
        </div>

2 个答案:

答案 0 :(得分:3)

您可以使用ng-class。这是一个jsBin,证明它有效。我已将justified类更改为文本颜色为红色。

<div class="btn-group question__answer nav-tabs" ng-class="{justified: question.type == 'radio'}">
    <a class="btn {{question.type}}" ng-repeat="answer in question.answers">{{ answer }}</a>
</div>

答案 1 :(得分:1)

您可以使用ng-class指令:

<div class="btn-group question__answer justified nav-tabs" 
   ng-class="{question.type =='radio' ? 'radio-class : 'other-class'}">
            <a class="btn {{question.type}}" ng-repeat="answer in question.answers">{{ answer }}</a>
        </div>