我的問題很簡單,但請相信我,我一直試圖將我的頭圍繞它幾個小時。按類選擇器渲染Angular2組件
有一個組件需要通過類選擇器實例化。
@Component({
selector: '.mycomponent',
template: '<h1>hello!</h1>'
})
export class MyComponent{}
假設父組件是這樣的:
@Component({
...
template:
`
<div class="mycomponent"></div> <!-- rendered -->
<div [class]="'mycomponent'"></div> <!-- not rendered -->
`
})
export class ParentComponent{}
爲什麼第二個版本是從來沒有顯示?我將如何渲染它?這是一個變化檢測問題還是它不應該這樣工作? 我玩過變化檢測策略,但沒有任何效果。我也碰到過DynamicComponentLoader
。我的希望是,我可以避開它。
有什麼辦法通過非元素選擇器動態加載組件?
對於像'
是的,因爲動態HTML與您的示例一樣不適用於選擇器。 http://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468 –