0

是否可以通过将 ng-switch 替换为 ng-class 来避免在此处重复代码。

<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>
4

1 回答 1

5

而不是做一个 Angular 版本,你可以只创建一个 css 类.upload .checkbox等。它映射到icon-*

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

jsfiddle 上的示例

于 2013-06-25T11:41:55.933 回答