根据angularjs中的条件添加类?

时间:2015-06-04 08:18:10

标签: javascript angularjs conditional-statements

我想根据angularJs中的条件添加类,我遵循以下方法,它工作正常,有没有最好的做法?

<div ng-if="client.status != 2" class="list-primary">
                    <span class="pull-right amount">{{client.receivables|number:2}}</span>
                    <div class="name">{{client.name}}</div>
                </div>


                <div ng-if="client.status == 2" class="list-primary text-muted">
                    <span class="pull-right amount">{{client.receivables|number:2}}</span>
                    <div class="name">{{client.name}}</div>
                </div>

3 个答案:

答案 0 :(得分:4)

您可以使用ng-class

<!-- apply class 'text-muted' when client.status == 2 -->
<div class="list-primary" ng-class="{ 'text-muted': client.status == 2 }">
    <span class="pull-right amount">{{client.receivables|number:2}}</span>
    <div class="name">{{client.name}}</div>
</div>

答案 1 :(得分:4)

在角度 1.1.5 + 中,您可以使用javascript三元运算符。

<div ng-class="client.status == 2 ? 'text-muted' : 'other-class'" class="list-primary ">
     <span class="pull-right amount">{{client.receivables|number:2}}</span>
     <div class="name">{{client.name}}</div>
</div>

我希望这会有所帮助

答案 2 :(得分:1)

查看AngularJS中提供的#define XScale [[UIScreen mainScreen] bounds].size.width / 320.0f #define YScale [[UIScreen mainScreen] bounds].size.height / 568.0f 。文档:

What is the difference between single-quoted and double-quoted strings in PHP?