如何根据转发器中的值显示指令

时间:2013-10-11 20:26:01

标签: javascript angularjs

我有一个循环器,循环遍历一个类型列表。根据类型值,它将显示相应的指令。我有几个指令叫做:dropdown,Long text,Choice,multichoice等。转发器将循环遍历类型列表以显示匹配指令

<div ng-repeat="field in model.Fields">
     <!-- if field = text then render longtext directive -->
     <!-- if field = dropdown then render dropdown directive -->
     <!-- if field = choice then render choice directive -->
     <!-- and so on -->
</div>

有优雅的方法吗?顺便说一句,您可以将此邮件的标题更改为更合适的标题。

1 个答案:

答案 0 :(得分:2)

Angular的ng-switch指令可能更合适:

<div ng-repeat="field in model.Fields">
    <div ng-switch on="field">
        <div ng-switch-when="text" longtext>
            <!-- Render the longtext directive -->
        </div>
        <div ng-switch-when="dropdown" dropdown>
            <!-- Render the longtext directive -->
        </div>
        <div ng-switch-when="choice" choice>
            <!-- Render the longtext directive -->
        </div>
        <div ng-switch-default default>
            <!-- Render the default directive -->
        </div>
    </div>
</div>