是否可以组合多个ng级

时间:2013-06-25 11:23:29

标签: angularjs ng-class

是否可以通过用ng-class替换ng-switch来避免重复代码。

<li ng-repeat="el in elm.required">
    <span ng-switch on="el.type" >
        <i class="icon-file" ng-switch-when="upload" tooltip="{{el.name}}" ng-class="{'muted':!el.completed}"></i>
        <i class="icon-check" ng-switch-when="checkbox" tooltip="{{el.name}}" ng-class="{'muted':!el.completed}"></i>
        <i class="icon-calendar" ng-switch-when="date" tooltip="{{el.name}}" ng-class="{'muted':!el.completed}"></i>
        <i class="icon-pencil" ng-switch-when="text" tooltip="{{el.name}}" ng-class="{'muted':!el.completed}"></i>
        <i class="icon-envelope" ng-switch-when="email" tooltip="{{el.name}}" ng-class="{'muted':!el.completed}"></i>
    </span>
</li>

有这样的东西(不工作,因为有两个ng-class指令):

<li ng-repeat="el in elm.required">
    <i tooltip="{{el.name}}" ng-class="{upload:'icon\-file', checkbox:'icon\-check', 'date':'icon\-calendar', text:'icon\-pencil', 'email':'icon\-envelope'}[el.type]" ng-class="{'muted':!el.completed}" ></i>
</li>

1 个答案:

答案 0 :(得分:5)

您可以创建一个.upload .checkbox等的css类,而不是使用Angular版本,而是映射到icon-*

<li ng-repeat="el in elm.required">
    <i tooltip="{{el.name}}" class="{{el.type}}" ng-class="{'muted':!el.completed}" ></i>
</li>

Example on jsfiddle