Javascript Angular Component:没有模板替换选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35290332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular Component: no template replace option?
提问by Stepan Suvorov
It looks like there is no "replace" option in new AngularJS 1.5Component concept (like it was for directives).
看起来在新的AngularJS 1.5组件概念中没有“替换”选项(就像指令一样)。
What would you suggest if I want to have table row<TR>
element as component? Is it not possible in terms of valid HTML?
如果我想将表格行<TR>
元素作为组件,您有什么建议?就有效的 HTML 而言,这是不可能的吗?
Real example: mailBox component has mail components inside. By markup mail-box-component is table, and mail-box is tr.
真实例子:mailBox 组件内部有邮件组件。通过标记mail-box-component 是table,mail-box 是tr。
<mail-box>
<mail ng-repeat="mail in $ctrl.mails" mail="mail"></mail>
<mail-box>
UPD: related discussion about directives - Why is replace deprecated in AngularJS?
UPD:关于指令的相关讨论 -为什么在 AngularJS 中不推荐使用替换?
采纳答案by Patrick Kelleter
This is not possible the-angular-way anymore since the replace: true flag has been deprecated
由于 replace: true 标志已被弃用,因此这不再是可能的角度方式
Why is replace deprecated in AngularJS?
the replace: true flag had come up with more problems than solutions which is why it was removed. therefore you can not build directives in such a way anymore and provide valid table-tr-td markup.
replace: true 标志带来的问题多于解决方案,这就是它被删除的原因。因此您不能再以这种方式构建指令并提供有效的 table-tr-td 标记。
However, there are two reasons why this is not as bad as it looks:
然而,这并不像看起来那么糟糕有两个原因:
you can do everything you want to do without table, tr, td, etc. just using elements like div, span, etc. and some css on it
web-components (and directives were a first attempt to simulate them) are not meant to represent such small fragments of the markup. they are more thought of as a fully functional component actually doing something. so whatever you want to do with your tr that you think it's worth building an element-directive around it, it probably isnt.
你可以在没有 table、tr、td 等的情况下做任何你想做的事情。只需要使用 div、span 等元素和一些 css 就可以了
web-components(和指令是模拟它们的第一次尝试)并不意味着代表标记的这么小的片段。他们更多地被认为是一个功能齐全的组件,实际上是在做某事。所以无论你想用你的 tr 做什么,你认为值得围绕它构建一个元素指令,它可能不是。
Maybe, what you can do is using an attribute-directive instead:
也许,您可以做的是改用属性指令:
<tr my-mail-directive></tr>
and your my-mail-directive does the magic on the tr element
你的 my-mail-directive 对 tr 元素起到了神奇的作用