在AngularJS中,我使用ng-class的方式如下:
<div class="bigIcon" data-ng-click="PickUp()"
ng-class="{first:'classA', second:'classB', third:'classC', fourth:'classC'}[call.State]"/>
我想知道我是否可以使用if-else表达式来做类似的事情:
<div class="bigIcon" data-ng-click="PickUp()"
ng-class="{first:'classA', second:'classB', else:'classC'}[call.State]"/>
所以无论何时调用。状态不同于第一次或第二次使用classC和避免指定每个值?
上面的解决方案对我来说并不适用于有背景图像的类。我所做的是我创建了一个默认类(你需要在else中),并设置class='defaultClass',然后ng-class="{class1:abc,class2:xyz}"
<span class="booking_warning" ng-class="{ process_success: booking.bookingStatus == 'BOOKING_COMPLETED' || booking.bookingStatus == 'BOOKING_PROCESSED', booking_info: booking.bookingStatus == 'INSTANT_BOOKING_REQUEST_RECEIVED' || booking.bookingStatus == 'BOOKING_PENDING'}"> <strong>{{booking.bookingStatus}}</strong> </span>
注:处于condition状态的类应该覆盖默认类,即标记为!important的类