【发布时间】:2013-09-16 19:17:49
【问题描述】:
我有一个角度指令,我正在对其应用 ng-repeat。该指令的目的是用基于传递给原始指令的值的不同指令交换自身:
<content-type-directive type="item.type" ng-repeat="item in items"></content-type-directive>
变成
<type1-specific-directive></type1-specific-directive>
<type2-specific-directive></type2-specific-directive>
<type1-specific-directive></type1-specific-directive>
<type1-specific-directive></type1-specific-directive>
...
完整示例:http://jsfiddle.net/qPGZ8/6/
如果第二个指令的 html 可以在第一个指令的包装元素内呈现,这将是直截了当的,但在这种情况下,我需要它来实际替换原始项目。
这与Customizing the template within a Directive有关
在 Misko 的回答中,他解释说使用 element.replaceWith() 换出原始 html。当项目不在 ng-repeat 中时,这可以正常工作,但在 ng-repeat 中它会破坏 ng-repeat 使用新元素更新自身的能力。
【问题讨论】:
标签: javascript html angularjs angularjs-directive