angular - 包含自定义组件 html 的角度管道
问题描述
想象一个这样的管道:
@Pipe({ name: 'addIcon' })
export class AddIconPipe implements PipeTransform {
constructor() {}
transform(value) {
return (
`
<div class="icon">
<app-icon name="welcome"></app-icon>
</div>
` + value
);
}
当你像这样使用这个管道时,{{ 'Garry' | appIcon }}
你会得到下一个结果:
<div class="icon">
</div>
Garry
但预期的结果是:
<div class="icon">
<app-icon>
contents of the app-icon component..., for example
<img src="welcome.jpg" />
</app-icon>
</div>
Garry
似乎管道中的自定义组件根本不呈现。
如何使用管道呈现自定义组件 html?
解决方案
老实说,我找不到任何你喜欢从管道而不是从Directive
.
ElementRef
您可以在directive
ctor 中注入。
管道应该只转换值,并且可以通过纯粹的方式优化转换,并且不会为对象的每次更改重新计算。
推荐阅读
- python - 如何使用 asyncio 在异步生成器上进行“轮询”或“选择”
- javascript - 如何在数组中添加和替换对象的元素
- hellosign-api - HelloSign:如何通过 API 复制模板?
- protractor - 如何运行 Protractor 文件夹中的所有测试而不将其作为套件添加到配置文件中
- python - 如何在 PySpark 中创建空 Spark DataFrame 并附加数据?
- angular - RXJS - 在后台如何实现删除旧值?
- python - 在seaborn中绘制散点图时出错
- null - 如何在 cython 中检查 Null 中的内存视图是否存在
- nativescript - 为什么 Nativescript 静态绑定生成器会无缘无故、没有信息地失败?
- javascript - HTML - 如何滚动整个部分而不在两者之间停下来?