在本文档中:http://docs.angularjs.org/guide/directive,它指示伪指令具有replace
配置:
JavaScript代码
app.directive('myd1', function(){
return {
template: '<span>directive template1</span>',
replace: true
}
});
app.directive('myd2', function(){
return {
template: '<span>directive template2</span>',
replace: false
}
});
html代码
<div myd1>
original content should be replaced
</div>
<div myd2>
original content should NOT be replaced
</div>
但是最后一页看起来像:
directive template1
directive template2
似乎
replace
不起作用。我想念什么吗?现场演示:http://plnkr.co/edit/rGIgmjO81X2UxJohL4HM?p=preview
最佳答案
您对transclude: true
感到困惑,后者会附加内部内容。replace: true
表示指令模板的内容将替换声明了指令的元素,在本例中为<div myd1>
标记。
http://plnkr.co/edit/k9qSx15fhSZRMwgAIMP4?p=preview
例如而没有 replace:true
<div myd1><span class="replaced" myd1="">directive template1</span></div>
和和
replace:true
<span class="replaced" myd1="">directive template1</span>
如您在后一个示例中看到的,div标签确实已被替换。
关于angularjs - 如何使用指令定义的 `replace`?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15285635/