[英]How to get an angular directive to include & replace another directive?
I have an AngularJS directive, directiveFoo
instantiated by an attribute: 我有一个AngularJS指令,由一个属性实例化的
directiveFoo
:
<div directiveFoo></div>
I want my directiveFoo
to always instantiate another directive, eg the template is: 我希望我的
directiveFoo
始终实例化另一个指令,例如模板是:
<div directiveBar="123"></div>
My question is this -- Is this possible to do without creating all the wasted nodes? 我的问题是这是否可以不创建所有浪费的节点? I'd like to add
replace
to my directive, but then it blows away directiveFoo
我想为我的指令添加
replace
,但随后它会吹掉directiveFoo
What I ultimately want is: 我最终想要的是:
<div directiveFoo directiveBar="123"></div>
with the two controllers attached. 连接两个控制器。
This is a simplified example. 这是一个简化的例子。 I'm trying to prevent:
我试图阻止:
<div directiveA>
<div directiveB>
<div directiveC="123">
<div directiveD></div>
</div>
</div>
</div>
in cases where I don't really need all the nested DOM nodes 在我不需要所有嵌套DOM节点的情况下
but then it blows away directiveFoo
但随后它吹走了指令.Foo
It shouldn't. 它不应该。 "The replacement process migrates all of the attributes / classes from the old element to the new one. " -- directive doc
“替换过程将所有属性/类从旧元素迁移到新元素。” - 指令doc
Because attributes are migrated, directive-foo
should show up in the generated HTML. 由于迁移了属性,因此
directive-foo
应显示在生成的HTML中。
app.directive('directiveFoo', function() {
return {
template: '<div directive-bar="123">bar</div>',
replace: true,
}
});
The above directive results in the following HTML (tested in Chrome): 上述指令产生以下HTML(在Chrome中测试):
<div directive-bar="123" directive-foo>bar</div>
In the fiddle I also create a controller for each directive (since you mentioned you wanted that), and I show that directive-bar
can access directive-foo
's controller. 在小提琴中,我还为每个指令创建了一个控制器(因为你提到了你想要的那个),并且我表明
directive-bar
可以访问directive-foo
的控制器。
(I put bar
as text in the template, just to make it easier to right-click on it and select "Inspect element"). (我把
bar
作为文本放在模板中,只是为了更容易右键单击它并选择“Inspect element”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.