通过类选择器呈现Angular2组件
我的问题很简单,但请相信我,我已经想了好几个小时了 有一个组件将通过类选择器实例化通过类选择器呈现Angular2组件,angular,angular2-template,Angular,Angular2 Template,我的问题很简单,但请相信我,我已经想了好几个小时了 有一个组件将通过类选择器实例化 @Component({ selector: '.mycomponent', template: '<h1>hello!</h1>' }) export class MyComponent{} @组件({ 选择器:'.mycomponent', 模板:“你好!” }) 导出类MyComponent{} 假设父组件如下所示: @Component({ ... templa
@Component({
selector: '.mycomponent',
template: '<h1>hello!</h1>'
})
export class MyComponent{}
@组件({
选择器:'.mycomponent',
模板:“你好!”
})
导出类MyComponent{}
假设父组件如下所示:
@Component({
...
template:
`
<div class="mycomponent"></div> <!-- rendered -->
<div [class]="'mycomponent'"></div> <!-- not rendered -->
`
})
export class ParentComponent{}
@组件({
...
模板:
`
`
})
导出类ParentComponent{}
为什么第二个版本从未被渲染?我该怎么做?这是一个变更检测问题,还是不应该以这种方式工作?
我已经尝试过变更检测策略,但没有任何效果。我还遇到了DynamicComponentLoader
。我的希望是我可以到处使用它
有没有办法通过非元素选择器动态加载组件?这是不应该工作的。组件和指令仅应用于静态添加的标记、属性和类
如果您想动态添加/删除组件和指令,请使用或
createComponent()
的为什么它不起作用是因为当您使用[class]时,它的意思是“class”是一个属性,而不是将“mycomponent”类分配给div。对于类似于:Yes,因为像您的示例那样的动态HTML不适用于选择器。另见