AngularJS 组件可以修改它们的包装元素吗?
Can AngularJS components modify their wrapper element?
我喜欢将 Angular 组件视为元素指令。给定一个名为 hero
的组件,我可以像这样在父模板中使用它:
<hero myparam="something"></hero>
我想将 hero
元素用作组件管理的容器,组件负责整个元素。
预期
这是我希望从上面的绑定中得到的:
<hero id="component123" class="alien" custom="foo">text</hero>
我的自定义组件转换给定元素并按其认为合适的方式使用它。
实际
但是,组件似乎只能在英雄元素内部 呈现其模板。我能得到的最好的是:
<hero myparam="something">
<div id="component123" class="alien" custom="foo">
text
</div>
</hero>
我觉得这很糟糕,因为 hero 元素实际上并不是英雄,而只是实际英雄的包装。这会混淆语义并创建不需要的额外元素。
Angular 中的最佳做法是将组件用作纯包装器并将实际组件放入其中吗?
这里有一个可以试用的官方示例:
https://plnkr.co/edit/NKjCDS8OEngYHNrBmC5O?p=preview
I like to think of Angular components as element directives.
组件可以被认为是一种更新模板的特殊指令。如果要更改属性,请使用实际指令(属性指令)
My custom component transforms the given element and uses it as it
sees fit.
当您谈论转换时,您需要的是指令,而不是组件。
我喜欢将 Angular 组件视为元素指令。给定一个名为 hero
的组件,我可以像这样在父模板中使用它:
<hero myparam="something"></hero>
我想将 hero
元素用作组件管理的容器,组件负责整个元素。
预期
这是我希望从上面的绑定中得到的:
<hero id="component123" class="alien" custom="foo">text</hero>
我的自定义组件转换给定元素并按其认为合适的方式使用它。
实际
但是,组件似乎只能在英雄元素内部 呈现其模板。我能得到的最好的是:
<hero myparam="something">
<div id="component123" class="alien" custom="foo">
text
</div>
</hero>
我觉得这很糟糕,因为 hero 元素实际上并不是英雄,而只是实际英雄的包装。这会混淆语义并创建不需要的额外元素。
Angular 中的最佳做法是将组件用作纯包装器并将实际组件放入其中吗?
这里有一个可以试用的官方示例: https://plnkr.co/edit/NKjCDS8OEngYHNrBmC5O?p=preview
I like to think of Angular components as element directives.
组件可以被认为是一种更新模板的特殊指令。如果要更改属性,请使用实际指令(属性指令)
My custom component transforms the given element and uses it as it sees fit.
当您谈论转换时,您需要的是指令,而不是组件。