angular - 模板中的角度变量选择器
问题描述
我觉得我想做的事情应该很简单,但我们在这里......
基本上我想使用许多可能的选择器之一来添加许多组件之一。
我可能可以使用这样的 ng-switch
<div ng-switch="template">
<div ng-switch-when="comp-1">
<comp-1></comp-1>
</div>
<div ng-switch-when="comp-2">
<comp-2></comp-2>
</div>
<div ng-switch-when="comp-3">
<comp-3></comp-3>
</div>
<div ng-switch-default>
<h1>Hey now you need to go here ad add your comp which is annoying!</h1>
</div>
</div>
但通常必须有一种方法来做这样的事情:
<{{template}}></{{template}}>
所以我只需要将字符串“comp-2”作为“模板”传入,它就会生成一个组件。
解决方案
我推荐使用动态组件生成技术,所以创建一个像这样的组件:
<app-custom [componentname]='comp-3'></app-custom>
export class CustomComponent implements OnInit {
@Input() componentname: any;
@ViewChild('myVar') myVar:ElementRef;
constructor() { }
ngOnInit() {
switch(this.componentname)
{
case "comp-3":
{
this.myVar = //Generate component
}
}
}
}
推荐阅读
- reactjs - 在 React Hooks 中输入文本时,antd Select Option 不呈现
- blazor-webassembly - Blazor WASM TargetFrameworks 网络标准 2.1 和 net5.0
- javascript - 单击按钮时切换脚本执行
- android - 无法加载 github.com 的信息
- assembly - 二叉搜索树插入程序集 x86
- css - @media 查询中的 CSS 字体大小在旋转后不会变回
- javascript - 谷歌表格隐藏网址
- configuration - 这个“编辑小部件”视图(适用于 iOS 14 小部件)的名称是什么?我该如何定义这样的东西?
- java - java继承子类扩展到父类时没有返回值
- c++ - 在没有宏的情况下在 C++ 中执行此部分函数应用程序的“正确”方法是什么?