如何用ng-transclude替换元素
是否可以将元素替换为ng-transclude
而不是整个模板元素?
Is it possible to replace the element with ng-transclude
on it rather than the entire template element?
HTML:
<div my-transcluded-directive>
<div>{{someData}}</div>
</div>
指令:
return {
restrict:'A',
templateUrl:'templates/my-transcluded-directive.html',
transclude:true,
link:function(scope,element,attrs)
{
}
};
my-transcluded-directive.html:
<div>
<div ng-transclude></div>
<div>I will not be touched.</div>
</div>
我正在寻找的是一种将<div>{{someData}}</div>
替换为<div ng-transclude></div>
的方法.当前发生的情况是将嵌入的HTML放置在
What I am looking for is a way to have <div>{{someData}}</div>
replace <div ng-transclude></div>
. What currently happens is the transcluded HTML is placed inside the ng-transclude
div element.
有可能吗?
我认为最好的解决方案可能是创建自己的transclude-replace指令来处理此问题.但是对于您的示例的快速而肮脏的解决方案,您基本上可以手动将包含结果的结果放在所需的位置:
I think the best solution would probably be to create your own transclude-replace directive that would handle this. But for a quick and dirty solution to your example you could essentially manually place the result of the transclusion where you want:
my-transcluded-directive.html:
<div>
<span>I WILL BE REPLACED</span>
<div>I will not be touched.</div>
</div>
指令:
return {
restrict:'A',
templateUrl:'templates/my-transcluded-directive.html',
transclude:true,
link:function(scope,element,attrs,ctrl, transclude)
{
element.find('span').replaceWith(transclude());
}
};