0

我有一个要重构的 ng-switch 语句。它看起来像这样:

<div ng-switch on="property.type">
    <input type="text" ng-switch-when="string" ng-model="property.value" placeholder="Property value">
    <select ng-switch-when="bool" ng-model="property.value" ng-options="value for value in [true,false]" placeholder="Property value"></select>
    <input type="number" ng-switch-when="int" ng-model="property.value" placeholder="Property value">
    <input type="text" ng-switch-default ng-model="property.value" placeholder="Property value">
</div>

您会注意到ng-modelplaceholder选项在所有情况下都是相同的。我的问题是:我可以以某种方式重构它以使这些选项只写一次吗?

谢谢!乌里

4

1 回答 1

0

虽然没有完全重构以避免所有重复,但您可以通过组合输入标签来减少它:

<div ng-switch on="property.type">
  <select ng-switch-when="bool" ng-model="property.value" ng-options="value for value in [true,false]" placeholder="Property value"></select>
  <input type="{{property.type == 'int' ? 'number' : 'string'}}" ng-switch-default ng-model="property.value" placeholder="Property value">
</div>

这可以在http://plnkr.co/edit/FybDC1I4U7rSC6GZJ3az看到

如果这是我正在做的一个项目,我怀疑我不会进一步重构它,留下少量重复的标记,以保持清晰。

于 2014-10-12T18:53:18.813 回答